home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / text / edit / vim60src.lha / Vim / vim60 / src / syntax.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-26  |  211.1 KB  |  8,306 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  * See README.txt for an overview of the Vim source code.
  8.  */
  9.  
  10. /*
  11.  * syntax.c: code for syntax highlighting
  12.  */
  13.  
  14. #include "vim.h"
  15.  
  16. /*
  17.  * Structure that stores information about a highlight group.
  18.  * The ID of a highlight group is also called group ID.  It is the index in
  19.  * the highlight_ga array PLUS ONE.
  20.  */
  21. struct hl_group
  22. {
  23.     char_u    *sg_name;    /* highlight group name */
  24.     char_u    *sg_name_u;    /* uppercase of sg_name */
  25. /* for normal terminals */
  26.     int        sg_term;    /* "term=" highlighting attributes */
  27.     char_u    *sg_start;    /* terminal string for start highl */
  28.     char_u    *sg_stop;    /* terminal string for stop highl */
  29.     int        sg_term_attr;    /* Screen attr for term mode */
  30. /* for color terminals */
  31.     int        sg_cterm;    /* "cterm=" highlighting attr */
  32.     int        sg_cterm_bold;    /* bold attr was set for light color */
  33.     int        sg_cterm_fg;    /* terminal fg color number + 1 */
  34.     int        sg_cterm_bg;    /* terminal bg color number + 1 */
  35.     int        sg_cterm_attr;    /* Screen attr for color term mode */
  36. #ifdef FEAT_GUI
  37. /* for when using the GUI */
  38.     int        sg_gui;        /* "gui=" highlighting attributes */
  39.     guicolor_T    sg_gui_fg;    /* GUI foreground color handle + 1 */
  40.     char_u    *sg_gui_fg_name;/* GUI foreground color name */
  41.     guicolor_T    sg_gui_bg;    /* GUI background color handle + 1 */
  42.     char_u    *sg_gui_bg_name;/* GUI background color name */
  43.     GuiFont    sg_font;    /* GUI font handle */
  44. #ifdef FEAT_XFONTSET
  45.     GuiFontset    sg_fontset;    /* GUI fontset handle */
  46. #endif
  47.     char_u    *sg_font_name;  /* GUI font or fontset name */
  48.     int        sg_gui_attr;    /* Screen attr for GUI mode */
  49. #endif
  50.     int        sg_link;    /* link to this highlight group ID */
  51.     int        sg_set;        /* combination of SG_* flags */
  52. };
  53.  
  54. #define SG_TERM        1    /* term has been set */
  55. #define SG_CTERM    2    /* cterm has been set */
  56. #define SG_GUI        4    /* gui has been set */
  57. #define SG_LINK        8    /* link has been set */
  58.  
  59. static garray_T highlight_ga;    /* highlight groups for 'highlight' option */
  60.  
  61. #define HL_TABLE() ((struct hl_group *)((highlight_ga.ga_data)))
  62.  
  63. #ifdef FEAT_CMDL_COMPL
  64. static int include_default = FALSE;    /* include "default" for expansion */
  65. static int include_link = FALSE;    /* include "link" for expansion */
  66. #endif
  67.  
  68. /*
  69.  * The "term", "cterm" and "gui" arguments can be any combination of the
  70.  * following names, separated by commas (but no spaces!).
  71.  */
  72. static char *(hl_name_table[]) =
  73.     {"bold", "standout", "underline", "italic", "reverse", "inverse", "NONE"};
  74. static int hl_attr_table[] =
  75.     {HL_BOLD, HL_STANDOUT, HL_UNDERLINE, HL_ITALIC, HL_INVERSE, HL_INVERSE, 0};
  76.  
  77. static int get_attr_entry  __ARGS((garray_T *table, attrentry_T *aep));
  78. static void syn_unadd_group __ARGS((void));
  79. static void set_hl_attr __ARGS((int idx));
  80. static void highlight_list_one __ARGS((int id));
  81. static int highlight_list_arg __ARGS((int id, int didh, int type, int iarg, char_u *sarg, char *name));
  82. static int syn_add_group __ARGS((char_u *name));
  83. static int syn_list_header __ARGS((int did_header, int outlen, int id));
  84. static int hl_has_settings __ARGS((int idx, int check_link));
  85. static void highlight_clear __ARGS((int idx));
  86.  
  87. #ifdef FEAT_GUI
  88. static void gui_do_one_color __ARGS((int idx, int do_menu, int do_tooltip));
  89. static int  set_group_colors __ARGS((char_u *name, guicolor_T *fgp, guicolor_T *bgp, int do_menu, int use_norm, int do_tooltip));
  90. static guicolor_T color_name2handle __ARGS((char_u *name));
  91. static GuiFont font_name2handle __ARGS((char_u *name));
  92. # ifdef FEAT_XFONTSET
  93. static GuiFontset fontset_name2handle __ARGS((char_u *name, int fixed_width));
  94. # endif
  95. static void hl_do_font __ARGS((int idx, char_u *arg, int do_normal, int do_menu, int do_tooltip));
  96. #endif
  97.  
  98. /*
  99.  * An attribute number is the index in attr_table plus ATTR_OFF.
  100.  */
  101. #define ATTR_OFF (HL_ALL + 1)
  102.  
  103. #if defined(FEAT_SYN_HL) || defined(PROTO)
  104.  
  105. #define SYN_NAMELEN    50        /* maximum length of a syntax name */
  106.  
  107. /* different types of offsets that are possible */
  108. #define SPO_MS_OFF    0    /* match  start offset */
  109. #define SPO_ME_OFF    1    /* match  end    offset */
  110. #define SPO_HS_OFF    2    /* highl. start offset */
  111. #define SPO_HE_OFF    3    /* highl. end    offset */
  112. #define SPO_RS_OFF    4    /* region start offset */
  113. #define SPO_RE_OFF    5    /* region end    offset */
  114. #define SPO_LC_OFF    6    /* leading context offset */
  115. #define SPO_COUNT    7
  116.  
  117. static char *(spo_name_tab[SPO_COUNT]) =
  118.         {"ms=", "me=", "hs=", "he=", "rs=", "re=", "lc="};
  119.  
  120. /*
  121.  * The patterns that are being searched for are stored in a syn_pattern.
  122.  * A match item consists of one pattern.
  123.  * A start/end item consists of n start patterns and m end patterns.
  124.  * A start/skip/end item consists of n start patterns, one skip pattern and m
  125.  * end patterns.
  126.  * For the latter two, the patterns are always consecutive: start-skip-end.
  127.  *
  128.  * A character offset can be given for the matched text (_m_start and _m_end)
  129.  * and for the actually highlighted text (_h_start and _h_end).
  130.  */
  131. typedef struct syn_pattern
  132. {
  133.     char     sp_type;        /* see SPTYPE_ defines below */
  134.     char     sp_syncing;        /* this item used for syncing */
  135.     short     sp_flags;        /* see HL_ defines below */
  136.     struct sp_syn sp_syn;        /* struct passed to in_id_list() */
  137.     short     sp_syn_match_id;    /* highlight group ID of pattern */
  138.     char_u    *sp_pattern;        /* regexp to match, pattern */
  139.     regprog_T    *sp_prog;        /* regexp to match, program */
  140.     int         sp_ic;            /* ignore-case flag for sp_prog */
  141.     short     sp_off_flags;        /* see below */
  142.     int         sp_offsets[SPO_COUNT];    /* offsets */
  143.     short    *sp_cont_list;        /* cont. group IDs, if non-zero */
  144.     short    *sp_next_list;        /* next group IDs, if non-zero */
  145.     int         sp_sync_idx;        /* sync item index (syncing only) */
  146.     int         sp_line_id;        /* ID of last line where tried */
  147.     int         sp_startcol;        /* next match in sp_line_id line */
  148. } synpat_T;
  149.  
  150. /* The sp_off_flags are computed like this:
  151.  * offset from the start of the matched text: (1 << SPO_XX_OFF)
  152.  * offset from the end     of the matched text: (1 << (SPO_XX_OFF + SPO_COUNT))
  153.  * When both are present, only one is used.
  154.  */
  155.  
  156. #define SPTYPE_MATCH    1    /* match keyword with this group ID */
  157. #define SPTYPE_START    2    /* match a regexp, start of item */
  158. #define SPTYPE_END    3    /* match a regexp, end of item */
  159. #define SPTYPE_SKIP    4    /* match a regexp, skip within item */
  160.  
  161. #define HL_CONTAINED    0x01    /* not used on toplevel */
  162. #define HL_TRANSP    0x02    /* has no highlighting    */
  163. #define HL_ONELINE    0x04    /* match within one line only */
  164. #define HL_HAS_EOL    0x08    /* end pattern that matches with $ */
  165. #define HL_SYNC_HERE    0x10    /* sync point after this item (syncing only) */
  166. #define HL_SYNC_THERE    0x20    /* sync point at current line (syncing only) */
  167. #define HL_MATCH    0x40    /* use match ID instead of item ID */
  168. #define HL_SKIPNL    0x80    /* nextgroup can skip newlines */
  169. #define HL_SKIPWHITE    0x100    /* nextgroup can skip white space */
  170. #define HL_SKIPEMPTY    0x200    /* nextgroup can skip empty lines */
  171. #define HL_KEEPEND    0x400    /* end match always kept */
  172. #define HL_EXCLUDENL    0x800    /* exclude NL from match */
  173. #define HL_DISPLAY    0x1000    /* only used for displaying, not syncing */
  174. #define HL_FOLD        0x2000    /* define fold */
  175. #define HL_EXTEND    0x4000    /* ignore a keepend */
  176. #define HL_MATCHCONT    0x8000    /* match continued from previous line */
  177.  
  178. #define SYN_ITEMS(buf)    ((synpat_T *)((buf)->b_syn_patterns.ga_data))
  179.  
  180. #define NONE_IDX    -2    /* value of sp_sync_idx for "NONE" */
  181.  
  182. /*
  183.  * Flags for b_syn_sync_flags:
  184.  */
  185. #define SF_CCOMMENT    0x01    /* sync on a C-style comment */
  186. #define SF_MATCH    0x02    /* sync by matching a pattern */
  187.  
  188. #define SYN_STATE_P(ssp)    ((bufstate_T *)((ssp)->ga_data))
  189.  
  190. /*
  191.  * Settings for keyword hash table.  It uses a simplistic hash function: add
  192.  * all characters together, modulo KHASH_SIZE.
  193.  */
  194. #define KHASH_SIZE    512
  195. #define KHASH_MASK    (KHASH_SIZE - 1)
  196. #define MAXKEYWLEN    80        /* maximum length of a keyword */
  197.  
  198. /*
  199.  * The attributes of the syntax item that has been recognized.
  200.  */
  201. static int current_attr = 0;        /* attr of current syntax word */
  202. #ifdef FEAT_EVAL
  203. static int current_id = 0;        /* ID of current char for syn_get_id() */
  204. static int current_trans_id = 0;    /* idem, transparancy removed */
  205. #endif
  206.  
  207. struct syn_cluster
  208. {
  209.     char_u        *scl_name;        /* syntax cluster name */
  210.     char_u        *scl_name_u;    /* uppercase of scl_name */
  211.     short        *scl_list;        /* IDs in this syntax cluster */
  212. };
  213.  
  214. /*
  215.  * Methods of combining two clusters
  216.  */
  217. #define CLUSTER_REPLACE        1    /* replace first list with second */
  218. #define CLUSTER_ADD        2    /* add second list to first */
  219. #define CLUSTER_SUBTRACT    3    /* subtract second list from first */
  220.  
  221. #define SYN_CLSTR(buf)    ((struct syn_cluster *)((buf)->b_syn_clusters.ga_data))
  222.  
  223. /*
  224.  * Syntax group IDs have different types:
  225.  *     0 -  9999  normal syntax groups
  226.  * 10000 - 14999  ALLBUT indicator (current_syn_inc_tag added)
  227.  * 15000 - 19999  TOP indicator (current_syn_inc_tag added)
  228.  * 20000 - 24999  CONTAINED indicator (current_syn_inc_tag added)
  229.  * >= 25000      cluster IDs (subtract SYNID_CLUSTER for the cluster ID)
  230.  */
  231. #define SYNID_ALLBUT    10000        /* syntax group ID for contains=ALLBUT */
  232. #define SYNID_TOP    15000        /* syntax group ID for contains=TOP */
  233. #define SYNID_CONTAINED    20000        /* syntax group ID for contains=CONTAINED */
  234. #define SYNID_CLUSTER    25000        /* first syntax group ID for clusters */
  235.  
  236. /*
  237.  * Annoying Hack(TM):  ":syn include" needs this pointer to pass to
  238.  * expand_filename().  Most of the other syntax commands don't need it, so
  239.  * instead of passing it to them, we stow it here.
  240.  */
  241. static char_u **syn_cmdlinep;
  242.  
  243. /*
  244.  * Another Annoying Hack(TM):  To prevent rules from other ":syn include"'d
  245.  * files from from leaking into ALLBUT lists, we assign a unique ID to the
  246.  * rules in each ":syn include"'d file.
  247.  */
  248. static int current_syn_inc_tag = 0;
  249. static int running_syn_inc_tag = 0;
  250.  
  251. /*
  252.  * To reduce the time spent in keepend(), remember at which level in the state
  253.  * stack the first item with "keepend" is present.  When "-1", there is no
  254.  * "keepend" on the stack.
  255.  */
  256. static int keepend_level = -1;
  257.  
  258. /*
  259.  * For the current state we need to remember more than just the idx.
  260.  * When si_m_endpos.lnum is 0, the items other than si_idx are unknown.
  261.  * (The end positions have the column number of the next char)
  262.  */
  263. typedef struct state_item
  264. {
  265.     int        si_idx;            /* index of syntax pattern */
  266.     int        si_id;            /* highlight group ID for keywords */
  267.     int        si_trans_id;        /* idem, transparancy removed */
  268.     int        si_m_lnum;        /* lnum of the match */
  269.     int        si_m_startcol;        /* starting column of the match */
  270.     lpos_T    si_m_endpos;        /* just after end posn of the match */
  271.     lpos_T    si_h_startpos;        /* start position of the highlighting */
  272.     lpos_T    si_h_endpos;        /* end position of the highlighting */
  273.     lpos_T    si_eoe_pos;        /* end position of end pattern */
  274.     int        si_end_idx;        /* group ID for end pattern or zero */
  275.     int        si_ends;        /* if match ends before si_m_endpos */
  276.     int        si_attr;        /* attributes in this state */
  277.     int        si_flags;        /* HL_HAS_EOL flag in this state, and
  278.                      * HL_SKIP* for si_next_list */
  279.     short    *si_cont_list;        /* list of contained groups */
  280.     short    *si_next_list;        /* nextgroup IDs after this item ends */
  281.     reg_extmatch_T *si_extmatch;    /* \z(...\) matches from start
  282.                      * pattern */
  283. } stateitem_T;
  284.  
  285. #define KEYWORD_IDX    -1        /* value of si_idx for keywords */
  286. #define ID_LIST_ALL    (short *)-1 /* valid of si_cont_list for containing all
  287.                        but contained groups */
  288.  
  289. /*
  290.  * The next possible match in the current line for any pattern is remembered,
  291.  * to avoid having to try for a match in each column.
  292.  * If next_match_idx == -1, not tried (in this line) yet.
  293.  * If next_match_col == MAXCOL, no match found in this line.
  294.  * (All end positions have the column of the char after the end)
  295.  */
  296. static int next_match_col;        /* column for start of next match */
  297. static lpos_T next_match_m_endpos;    /* position for end of next match */
  298. static lpos_T next_match_h_startpos;  /* pos. for highl. start of next match */
  299. static lpos_T next_match_h_endpos;    /* pos. for highl. end of next match */
  300. static int next_match_idx;        /* index of matched item */
  301. static int next_match_flags;        /* flags for next match */
  302. static lpos_T next_match_eos_pos;        /* end of start pattern (start of region) */
  303. static lpos_T next_match_eoe_pos;        /* pos. for end of end pattern */
  304. static int next_match_end_idx;        /* ID of group for end pattern or zero */
  305. static reg_extmatch_T *next_match_extmatch = NULL;
  306.  
  307. /*
  308.  * A state stack is an array of integers or stateitem_T, stored in a
  309.  * garray_T.  A state stack is invalid if it's itemsize entry is zero.
  310.  */
  311. #define INVALID_STATE(ssp)  ((ssp)->ga_itemsize == 0)
  312. #define VALID_STATE(ssp)    ((ssp)->ga_itemsize != 0)
  313.  
  314. /*
  315.  * The current state (within the line) of the recognition engine.
  316.  * When current_state.ga_itemsize is 0 the current state is invalid.
  317.  */
  318. static win_T    *syn_win;        /* current window for highlighting */
  319. static buf_T    *syn_buf;        /* current buffer for highlighting */
  320. static linenr_T current_lnum = 0;    /* lnum of current state */
  321. static colnr_T    current_col = 0;    /* column of current state */
  322. static int    current_state_stored = 0; /* TRUE if stored current state
  323.                        * after setting current_finished */
  324. static int    current_finished = 0;    /* current line has been finished */
  325. static garray_T current_state        /* current stack of state_items */
  326.         = {0, 0, 0, 0, NULL};
  327. static short    *current_next_list = NULL; /* when non-zero, nextgroup list */
  328. static int    current_next_flags = 0; /* flags for current_next_list */
  329. static int    current_line_id = 0;    /* unique number for current line */
  330.  
  331. #define CUR_STATE(idx)    ((stateitem_T *)(current_state.ga_data))[idx]
  332.  
  333. static void syn_sync __ARGS((win_T *wp, linenr_T lnum, synstate_T *last_valid));
  334. static int syn_match_linecont __ARGS((linenr_T lnum));
  335. static void syn_start_line __ARGS((void));
  336. static void syn_update_ends __ARGS((int startofline));
  337. static void syn_stack_alloc __ARGS((void));
  338. static int syn_stack_cleanup __ARGS((void));
  339. static void syn_stack_free_entry __ARGS((buf_T *buf, synstate_T *p));
  340. static synstate_T *syn_stack_find_entry __ARGS((linenr_T lnum));
  341. static synstate_T *store_current_state __ARGS((synstate_T *sp));
  342. static void load_current_state __ARGS((synstate_T *from));
  343. static void invalidate_current_state __ARGS((void));
  344. static int syn_stack_equal __ARGS((synstate_T *sp));
  345. static void validate_current_state __ARGS((void));
  346. static int syn_finish_line __ARGS((int syncing));
  347. static int syn_current_attr __ARGS((int syncing, int displaying));
  348. static int did_match_already __ARGS((int idx));
  349. static stateitem_T *push_next_match __ARGS((stateitem_T *cur_si));
  350. static void check_state_ends __ARGS((void));
  351. static void update_si_attr __ARGS((int idx));
  352. static void check_keepend __ARGS((void));
  353. static void update_si_end __ARGS((stateitem_T *sip, int startcol, int force));
  354. static short *copy_id_list __ARGS((short *list));
  355. static int in_id_list __ARGS((stateitem_T *item, short *cont_list, struct sp_syn *ssp, int contained));
  356. static int push_current_state __ARGS((int idx));
  357. static void pop_current_state __ARGS((void));
  358.  
  359. static void find_endpos __ARGS((int idx, lpos_T *startpos, lpos_T *m_endpos, lpos_T *hl_endpos, int *flagsp, lpos_T *end_endpos, int *end_idx, reg_extmatch_T *start_ext));
  360. static void clear_syn_state __ARGS((synstate_T *p));
  361. static void clear_current_state __ARGS((void));
  362.  
  363. static void limit_pos __ARGS((lpos_T *pos, lpos_T *limit));
  364. static void limit_pos_zero __ARGS((lpos_T *pos, lpos_T *limit));
  365. static void syn_add_end_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
  366. static void syn_add_start_off __ARGS((lpos_T *result, regmmatch_T *regmatch, synpat_T *spp, int idx, int extra));
  367. static char_u *syn_getcurline __ARGS((void));
  368. static int syn_regexec __ARGS((regmmatch_T *rmp, linenr_T lnum, colnr_T col));
  369. static int check_keyword_id __ARGS((char_u *line, int startcol, int *endcol, int *flags, short **next_list, stateitem_T *cur_si));
  370. static void syn_cmd_case __ARGS((exarg_T *eap, int syncing));
  371. static void syntax_sync_clear __ARGS((void));
  372. static void syn_remove_pattern __ARGS((buf_T *buf, int idx));
  373. static void syn_clear_pattern __ARGS((buf_T *buf, int i));
  374. static void syn_clear_cluster __ARGS((buf_T *buf, int i));
  375. static void syn_cmd_clear __ARGS((exarg_T *eap, int syncing));
  376. static void syn_clear_one __ARGS((int id, int syncing));
  377. static void syn_cmd_on __ARGS((exarg_T *eap, int syncing));
  378. static void syn_cmd_enable __ARGS((exarg_T *eap, int syncing));
  379. static void syn_cmd_reset __ARGS((exarg_T *eap, int syncing));
  380. static void syn_cmd_manual __ARGS((exarg_T *eap, int syncing));
  381. static void syn_cmd_off __ARGS((exarg_T *eap, int syncing));
  382. static void syn_cmd_onoff __ARGS((exarg_T *eap, char *name));
  383. static void syn_cmd_list __ARGS((exarg_T *eap, int syncing));
  384. static void syn_lines_msg __ARGS((void));
  385. static void syn_list_one __ARGS((int id, int syncing, int link_only));
  386. static void syn_list_cluster __ARGS((int id));
  387. static void put_id_list __ARGS((char_u *name, short *list, int attr));
  388. static void put_pattern __ARGS((char *s, int c, synpat_T *spp, int attr));
  389. static int syn_list_keywords __ARGS((int id, keyentry_T **ktabp, int did_header, int attr));
  390. static void syn_clear_keyword __ARGS((int id, keyentry_T **ktabp));
  391. static void free_keywtab __ARGS((keyentry_T **ktabp));
  392. static void add_keyword __ARGS((char_u *name, int id, int flags, short *cont_in_list, short *next_list));
  393. static int syn_khash __ARGS((char_u *p));
  394. static char_u *get_group_name __ARGS((char_u *arg, char_u **name_end));
  395. static char_u *get_syn_options __ARGS((char_u *arg, int *flagsp, int keyword, int *sync_idx, short **cont_list, short **cont_in_list, short **next_list));
  396. static void syn_cmd_include __ARGS((exarg_T *eap, int syncing));
  397. static void syn_cmd_keyword __ARGS((exarg_T *eap, int syncing));
  398. static void syn_cmd_match __ARGS((exarg_T *eap, int syncing));
  399. static void syn_cmd_region __ARGS((exarg_T *eap, int syncing));
  400. #ifdef __BORLANDC__
  401. static int _RTLENTRYF syn_compare_stub __ARGS((const void *v1, const void *v2));
  402. #else
  403. static int syn_compare_stub __ARGS((const void *v1, const void *v2));
  404. #endif
  405. static void syn_cmd_cluster __ARGS((exarg_T *eap, int syncing));
  406. static int syn_scl_name2id __ARGS((char_u *name));
  407. static int syn_scl_namen2id __ARGS((char_u *linep, int len));
  408. static int syn_check_cluster __ARGS((char_u *pp, int len));
  409. static int syn_add_cluster __ARGS((char_u *name));
  410. static void init_syn_patterns __ARGS((void));
  411. static char_u *get_syn_pattern __ARGS((char_u *arg, synpat_T *ci));
  412. static void syn_cmd_sync __ARGS((exarg_T *eap, int syncing));
  413. static int get_id_list __ARGS((char_u **arg, int keylen, short **list));
  414. static void syn_combine_list __ARGS((short **clstr1, short **clstr2, int list_op));
  415. static void syn_incl_toplevel __ARGS((int id, int *flagsp));
  416.  
  417. /*
  418.  * Start the syntax recognition for a line.  This function is normally called
  419.  * from the screen updating, once for each displayed line.
  420.  * The buffer is remembered in syn_buf, because get_syntax_attr() doesn't get
  421.  * it.    Careful: curbuf and curwin are likely to point to another buffer and
  422.  * window.
  423.  */
  424.     void
  425. syntax_start(wp, lnum)
  426.     win_T    *wp;
  427.     linenr_T    lnum;
  428. {
  429.     synstate_T    *p;
  430.     synstate_T    *last_valid = NULL;
  431.     synstate_T    *last_min_valid = NULL;
  432.     synstate_T    *sp, *prev;
  433.     linenr_T    parsed_lnum;
  434.     linenr_T    first_stored;
  435.     int        dist;
  436.  
  437.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  438.  
  439.     /*
  440.      * After switching buffers, invalidate current_state.
  441.      */
  442.     if (syn_buf != wp->w_buffer)
  443.     {
  444.     invalidate_current_state();
  445.     syn_buf = wp->w_buffer;
  446.     }
  447.     syn_win = wp;
  448.  
  449.     /*
  450.      * Allocate syntax stack when needed.
  451.      */
  452.     syn_stack_alloc();
  453.     if (syn_buf->b_sst_array == NULL)
  454.     goto theend;        /* out of memory */
  455.     syn_buf->b_sst_lasttick = display_tick;
  456.  
  457.     /*
  458.      * If the state of the end of the previous line is useful, store it.
  459.      */
  460.     if (VALID_STATE(¤t_state)
  461.         && current_lnum < lnum
  462.         && current_lnum < syn_buf->b_ml.ml_line_count)
  463.     {
  464.     (void)syn_finish_line(FALSE);
  465.     if (!current_state_stored)
  466.     {
  467.         ++current_lnum;
  468.         (void)store_current_state(NULL);
  469.     }
  470.  
  471.     /*
  472.      * If the current_lnum is now the same as "lnum", keep the current
  473.      * state (this happens very often!).  Otherwise invalidate
  474.      * current_state and figure it out below.
  475.      */
  476.     if (current_lnum != lnum)
  477.         invalidate_current_state();
  478.     }
  479.     else
  480.     invalidate_current_state();
  481.  
  482.     /*
  483.      * Try to synchronize from a saved state in b_sst_array[].
  484.      * Only do this if lnum is not before and not to far beyond a saved state.
  485.      */
  486.     if (INVALID_STATE(¤t_state) && syn_buf->b_sst_array != NULL)
  487.     {
  488.     /* Find last valid saved state before start_lnum. */
  489.     for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
  490.     {
  491.         if (p->sst_lnum > lnum)
  492.         break;
  493.         if (p->sst_lnum <= lnum && p->sst_change_lnum == 0)
  494.         {
  495.         last_valid = p;
  496.         if (p->sst_lnum >= lnum - syn_buf->b_syn_sync_minlines)
  497.             last_min_valid = p;
  498.         }
  499.     }
  500.     if (last_min_valid != NULL)
  501.         load_current_state(last_min_valid);
  502.     }
  503.  
  504.     /*
  505.      * If "lnum" is before or far beyond a line with a saved state, need to
  506.      * re-synchronize.
  507.      */
  508.     if (INVALID_STATE(¤t_state))
  509.     {
  510.     syn_sync(wp, lnum, last_valid);
  511.     first_stored = current_lnum + syn_buf->b_syn_sync_minlines;
  512.     }
  513.     else
  514.     first_stored = current_lnum;
  515.  
  516.     /*
  517.      * Advance from the sync point or saved state until the current line.
  518.      * Save some entries for syncing with later on.
  519.      */
  520.     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
  521.     prev = syn_stack_find_entry(current_lnum);
  522.     while (current_lnum < lnum)
  523.     {
  524.     syn_start_line();
  525.     (void)syn_finish_line(FALSE);
  526.     ++current_lnum;
  527.  
  528.     /* If we parsed at least "minlines" lines or started at a valid
  529.      * state, the current state is considered valid. */
  530.     if (current_lnum >= first_stored)
  531.     {
  532.         /* Check if the saved state entry is for the current line and is
  533.          * equal to the current state.  If so, then validate all saved
  534.          * states that depended on a change before the parsed line. */
  535.         if (prev == NULL)
  536.         sp = syn_buf->b_sst_first;
  537.         else
  538.         sp = prev->sst_next;
  539.         if (sp != NULL
  540.             && sp->sst_lnum == current_lnum
  541.             && syn_stack_equal(sp))
  542.         {
  543.         parsed_lnum = current_lnum;
  544.         prev = sp;
  545.         while (sp != NULL && sp->sst_change_lnum <= parsed_lnum)
  546.         {
  547.             if (sp->sst_lnum <= lnum)
  548.             /* valid state before desired line, use this one */
  549.             prev = sp;
  550.             else if (sp->sst_change_lnum == 0)
  551.             /* past saved states depending on change, break here. */
  552.             break;
  553.             sp->sst_change_lnum = 0;
  554.             sp = sp->sst_next;
  555.         }
  556.         load_current_state(prev);
  557.         }
  558.         /* Store the state at this line when it's the first one, the line
  559.          * where we start parsing, or some distance from the previously
  560.          * saved state.  But only when parsed at least 'minlines'. */
  561.         else if (prev == NULL
  562.             || current_lnum == lnum
  563.             || current_lnum >= prev->sst_lnum + dist)
  564.         prev = store_current_state(prev);
  565.     }
  566.  
  567.     /* This can take a long time: break when CTRL-C pressed.  The current
  568.      * state will be wrong then. */
  569.     line_breakcheck();
  570.     if (got_int)
  571.     {
  572.         current_lnum = lnum;
  573.         break;
  574.     }
  575.     }
  576.  
  577.     syn_start_line();
  578.  
  579. theend:
  580.     reg_syn = FALSE;
  581. }
  582.  
  583. /*
  584.  * We cannot simply discard growarrays full of state_items or buf_states; we
  585.  * have to manually release their extmatch pointers first.
  586.  */
  587.     static void
  588. clear_syn_state(p)
  589.     synstate_T *p;
  590. {
  591.     int        i;
  592.     garray_T    *gap;
  593.  
  594.     if (p->sst_stacksize > SST_FIX_STATES)
  595.     {
  596.     gap = &(p->sst_union.sst_ga);
  597.     for (i = 0; i < gap->ga_len; i++)
  598.         unref_extmatch(SYN_STATE_P(gap)[i].bs_extmatch);
  599.     ga_clear(gap);
  600.     }
  601.     else
  602.     {
  603.     for (i = 0; i < p->sst_stacksize; i++)
  604.         unref_extmatch(p->sst_union.sst_stack[i].bs_extmatch);
  605.     }
  606. }
  607.  
  608. /*
  609.  * Cleanup the current_state stack.
  610.  */
  611.     static void
  612. clear_current_state()
  613. {
  614.     int        i;
  615.     stateitem_T    *sip;
  616.  
  617.     sip = (stateitem_T *)(current_state.ga_data);
  618.     for (i = 0; i < current_state.ga_len; i++)
  619.     unref_extmatch(sip[i].si_extmatch);
  620.     ga_clear(¤t_state);
  621. }
  622.  
  623. /*
  624.  * Try to find a synchronisation point for line "lnum".
  625.  *
  626.  * This sets current_lnum and the current state.  One of three methods is
  627.  * used:
  628.  * 1. Search backwards for the end of a C-comment.
  629.  * 2. Search backwards for given sync patterns.
  630.  * 3. Simply start on a given number of lines above "lnum".
  631.  */
  632.     static void
  633. syn_sync(wp, start_lnum, last_valid)
  634.     win_T    *wp;
  635.     linenr_T    start_lnum;
  636.     synstate_T    *last_valid;
  637. {
  638.     buf_T    *curbuf_save;
  639.     win_T    *curwin_save;
  640.     pos_T    cursor_save;
  641.     int        idx;
  642.     linenr_T    lnum;
  643.     linenr_T    end_lnum;
  644.     linenr_T    break_lnum;
  645.     int        had_sync_point;
  646.     stateitem_T    *cur_si;
  647.     synpat_T    *spp;
  648.     char_u    *line;
  649.     int        found_flags = 0;
  650.     int        found_match_idx = 0;
  651.     linenr_T    found_current_lnum = 0;
  652.     int        found_current_col= 0;
  653.     lpos_T    found_m_endpos;
  654.  
  655.     /*
  656.      * Clear any current state that might be hanging around.
  657.      */
  658.     invalidate_current_state();
  659.  
  660.     /*
  661.      * Start at least "minlines" back.  Default starting point for parsing is
  662.      * there.
  663.      * Start further back, to avoid that scrolling backwards will result in
  664.      * resyncing for every line.  Now it resyncs only one out of N lines,
  665.      * where N is minlines * 1.5, or minlines * 2 if minlines is small.
  666.      * Watch out for overflow when minlines is MAXLNUM.
  667.      */
  668.     if (syn_buf->b_syn_sync_minlines > start_lnum)
  669.     start_lnum = 1;
  670.     else
  671.     {
  672.     if (syn_buf->b_syn_sync_minlines == 1)
  673.         lnum = 1;
  674.     else if (syn_buf->b_syn_sync_minlines < 10)
  675.         lnum = syn_buf->b_syn_sync_minlines * 2;
  676.     else
  677.         lnum = syn_buf->b_syn_sync_minlines * 3 / 2;
  678.     if (syn_buf->b_syn_sync_maxlines != 0
  679.                        && lnum > syn_buf->b_syn_sync_maxlines)
  680.         lnum = syn_buf->b_syn_sync_maxlines;
  681.     if (lnum >= start_lnum)
  682.         start_lnum = 1;
  683.     else
  684.         start_lnum -= lnum;
  685.     }
  686.     current_lnum = start_lnum;
  687.  
  688.     /*
  689.      * 1. Search backwards for the end of a C-style comment.
  690.      */
  691.     if (syn_buf->b_syn_sync_flags & SF_CCOMMENT)
  692.     {
  693.     /* Need to make syn_buf the current buffer for a moment, to be able to
  694.      * use find_start_comment(). */
  695.     curwin_save = curwin;
  696.     curwin = wp;
  697.     curbuf_save = curbuf;
  698.     curbuf = syn_buf;
  699.  
  700.     /*
  701.      * Skip lines that end in a backslash.
  702.      */
  703.     for ( ; start_lnum > 1; --start_lnum)
  704.     {
  705.         line = ml_get(start_lnum - 1);
  706.         if (*line == NUL || *(line + STRLEN(line) - 1) != '\\')
  707.         break;
  708.     }
  709.     current_lnum = start_lnum;
  710.  
  711.     /* set cursor to start of search */
  712.     cursor_save = wp->w_cursor;
  713.     wp->w_cursor.lnum = start_lnum;
  714.     wp->w_cursor.col = 0;
  715.  
  716.     /*
  717.      * If the line is inside a comment, need to find the syntax item that
  718.      * defines the comment.
  719.      * Restrict the search for the end of a comment to b_syn_sync_maxlines.
  720.      */
  721.     if (find_start_comment((int)syn_buf->b_syn_sync_maxlines) != NULL)
  722.     {
  723.         for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
  724.         if (SYN_ITEMS(syn_buf)[idx].sp_syn.id == syn_buf->b_syn_sync_id
  725.             && SYN_ITEMS(syn_buf)[idx].sp_type == SPTYPE_START)
  726.         {
  727.             validate_current_state();
  728.             if (push_current_state(idx) == OK)
  729.             update_si_attr(current_state.ga_len - 1);
  730.             break;
  731.         }
  732.     }
  733.  
  734.     /* restore cursor and buffer */
  735.     wp->w_cursor = cursor_save;
  736.     curwin = curwin_save;
  737.     curbuf = curbuf_save;
  738.     }
  739.  
  740.     /*
  741.      * 2. Search backwards for given sync patterns.
  742.      */
  743.     else if (syn_buf->b_syn_sync_flags & SF_MATCH)
  744.     {
  745.     if (syn_buf->b_syn_sync_maxlines != 0
  746.                  && start_lnum > syn_buf->b_syn_sync_maxlines)
  747.         break_lnum = start_lnum - syn_buf->b_syn_sync_maxlines;
  748.     else
  749.         break_lnum = 0;
  750.  
  751.     end_lnum = start_lnum;
  752.     lnum = start_lnum;
  753.     while (--lnum > break_lnum)
  754.     {
  755.         /* This can take a long time: break when CTRL-C pressed. */
  756.         line_breakcheck();
  757.         if (got_int)
  758.         {
  759.         invalidate_current_state();
  760.         current_lnum = start_lnum;
  761.         break;
  762.         }
  763.  
  764.         /* Check if we have run into a valid saved state stack now. */
  765.         if (last_valid != NULL && lnum == last_valid->sst_lnum)
  766.         {
  767.         load_current_state(last_valid);
  768.         break;
  769.         }
  770.  
  771.         /*
  772.          * Check if the previous line has the line-continuation pattern.
  773.          */
  774.         if (lnum > 1 && syn_match_linecont(lnum - 1))
  775.         continue;
  776.  
  777.         /*
  778.          * Start with nothing on the state stack
  779.          */
  780.         validate_current_state();
  781.  
  782.         for (current_lnum = lnum; current_lnum < end_lnum; ++current_lnum)
  783.         {
  784.         syn_start_line();
  785.         for (;;)
  786.         {
  787.             had_sync_point = syn_finish_line(TRUE);
  788.             /*
  789.              * When a sync point has been found, remember where, and
  790.              * continue to look for another one, further on in the line.
  791.              */
  792.             if (had_sync_point && current_state.ga_len)
  793.             {
  794.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  795.             if (cur_si->si_m_endpos.lnum > start_lnum)
  796.             {
  797.                 /* ignore match that goes to after where started */
  798.                 current_lnum = end_lnum;
  799.                 break;
  800.             }
  801.             spp = &(SYN_ITEMS(syn_buf)[cur_si->si_idx]);
  802.             found_flags = spp->sp_flags;
  803.             found_match_idx = spp->sp_sync_idx;
  804.             found_current_lnum = current_lnum;
  805.             found_current_col = current_col;
  806.             found_m_endpos = cur_si->si_m_endpos;
  807.             /*
  808.              * Continue after the match (be aware of a zero-length
  809.              * match).
  810.              */
  811.             if (found_m_endpos.lnum > current_lnum)
  812.             {
  813.                 current_lnum = found_m_endpos.lnum;
  814.                 current_col = found_m_endpos.col;
  815.                 if (current_lnum >= end_lnum)
  816.                 break;
  817.             }
  818.             else if (found_m_endpos.col > current_col)
  819.                 current_col = found_m_endpos.col;
  820.             else
  821.                 ++current_col;
  822.  
  823.             /* syn_current_attr() will have skipped the check for
  824.              * an item that ends here, need to do that now. */
  825.             ++current_col;
  826.             check_state_ends();
  827.             --current_col;
  828.             }
  829.             else
  830.             break;
  831.         }
  832.         }
  833.  
  834.         /*
  835.          * If a sync point was encountered, break here.
  836.          */
  837.         if (found_flags)
  838.         {
  839.         /*
  840.          * Put the item that was specified by the sync point on the
  841.          * state stack.  If there was no item specified, make the
  842.          * state stack empty.
  843.          */
  844.         clear_current_state();
  845.         if (found_match_idx >= 0
  846.             && push_current_state(found_match_idx) == OK)
  847.             update_si_attr(current_state.ga_len - 1);
  848.  
  849.         /*
  850.          * When using "grouphere", continue from the sync point
  851.          * match, until the end of the line.  Parsing starts at
  852.          * the next line.
  853.          * For "groupthere" the parsing starts at start_lnum.
  854.          */
  855.         if (found_flags & HL_SYNC_HERE)
  856.         {
  857.             if (current_state.ga_len)
  858.             {
  859.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  860.             cur_si->si_h_startpos.lnum = found_current_lnum;
  861.             cur_si->si_h_startpos.col = found_current_col;
  862.             update_si_end(cur_si, (int)current_col, TRUE);
  863.             check_keepend();
  864.             }
  865.             current_col = found_m_endpos.col;
  866.             current_lnum = found_m_endpos.lnum;
  867.             (void)syn_finish_line(FALSE);
  868.             ++current_lnum;
  869.         }
  870.         else
  871.             current_lnum = start_lnum;
  872.  
  873.         break;
  874.         }
  875.  
  876.         end_lnum = lnum;
  877.         invalidate_current_state();
  878.     }
  879.  
  880.     /* Ran into start of the file or exceeded maximum number of lines */
  881.     if (lnum <= break_lnum)
  882.     {
  883.         invalidate_current_state();
  884.         current_lnum = break_lnum + 1;
  885.     }
  886.     }
  887.  
  888.     validate_current_state();
  889. }
  890.  
  891. /*
  892.  * Return TRUE if the line-continuation pattern matches in line "lnum".
  893.  */
  894.     static int
  895. syn_match_linecont(lnum)
  896.     linenr_T    lnum;
  897. {
  898.     regmmatch_T regmatch;
  899.  
  900.     if (syn_buf->b_syn_linecont_prog != NULL)
  901.     {
  902.     regmatch.rmm_ic = syn_buf->b_syn_linecont_ic;
  903.     regmatch.regprog = syn_buf->b_syn_linecont_prog;
  904.     return syn_regexec(®match, lnum, (colnr_T)0);
  905.     }
  906.     return FALSE;
  907. }
  908.  
  909. /*
  910.  * Prepare the current state for the start of a line.
  911.  */
  912.     static void
  913. syn_start_line()
  914. {
  915.     current_finished = FALSE;
  916.     current_col = 0;
  917.  
  918.     /*
  919.      * Need to update the end of a start/skip/end that continues from the
  920.      * previous line and regions that have "keepend".
  921.      */
  922.     if (current_state.ga_len > 0)
  923.     syn_update_ends(TRUE);
  924.  
  925.     next_match_idx = -1;
  926.     ++current_line_id;
  927. }
  928.  
  929. /*
  930.  * Check for items in the stack that need their end updated.
  931.  * When "startofline" is TRUE the last item is always updated.
  932.  * When "startofline" is FALSE the item with "keepend" is forcefully updated.
  933.  */
  934.     static void
  935. syn_update_ends(startofline)
  936.     int        startofline;
  937. {
  938.     stateitem_T    *cur_si;
  939.     int        i;
  940.  
  941.     if (startofline)
  942.     {
  943.     /* Check for a match carried over from a previous line with a
  944.      * contained region.  The match ends as soon as the region ends. */
  945.     for (i = 0; i < current_state.ga_len; ++i)
  946.     {
  947.         cur_si = &CUR_STATE(i);
  948.         if ((SYN_ITEMS(syn_buf)[cur_si->si_idx]).sp_type == SPTYPE_MATCH
  949.             && cur_si->si_m_endpos.lnum < current_lnum)
  950.         {
  951.         cur_si->si_flags |= HL_MATCHCONT;
  952.         cur_si->si_m_endpos.lnum = 0;
  953.         cur_si->si_m_endpos.col = 0;
  954.         cur_si->si_h_endpos = cur_si->si_m_endpos;
  955.         cur_si->si_ends = TRUE;
  956.         }
  957.     }
  958.     }
  959.  
  960.     /*
  961.      * Need to update the end of a start/skip/end that continues from the
  962.      * previous line.  And regions that have "keepend", because they may
  963.      * influence contained items.
  964.      * Then check for items ending in column 0.
  965.      */
  966.     i = current_state.ga_len - 1;
  967.     if (keepend_level >= 0)
  968.     for ( ; i > keepend_level; --i)
  969.         if (CUR_STATE(i).si_flags & HL_EXTEND)
  970.         break;
  971.     for ( ; i < current_state.ga_len; ++i)
  972.     {
  973.     cur_si = &CUR_STATE(i);
  974.     if ((cur_si->si_flags & HL_KEEPEND)
  975.                 || (i == current_state.ga_len - 1 && startofline))
  976.     {
  977.         cur_si->si_h_startpos.col = 0;    /* start highl. in col 0 */
  978.         cur_si->si_h_startpos.lnum = current_lnum;
  979.  
  980.         if (!(cur_si->si_flags & HL_MATCHCONT))
  981.         update_si_end(cur_si, (int)current_col, !startofline);
  982.     }
  983.     }
  984.     check_keepend();
  985.     check_state_ends();
  986. }
  987.  
  988. /****************************************
  989.  * Handling of the state stack cache.
  990.  */
  991.  
  992. /*
  993.  * EXPLANATION OF THE SYNTAX STATE STACK CACHE
  994.  *
  995.  * To speed up syntax highlighting, the state stack for the start of some
  996.  * lines is cached.  These entries can be used to start parsing at that point.
  997.  *
  998.  * The stack is kept in b_sst_array[] for each buffer.  There is a list of
  999.  * valid entries.  b_sst_first points to the first one, then follow sst_next.
  1000.  * The entries are sorted on line number.  The first entry is often for line 2
  1001.  * (line 1 always starts with an empty stack).
  1002.  * There is also a list for free entries.  This construction is used to avoid
  1003.  * having to allocate and free memory blocks too often.
  1004.  *
  1005.  * When making changes to the buffer, this is logged in b_mod_*.  When calling
  1006.  * update_screen() to update the display, it will call
  1007.  * syn_stack_apply_changes() for each displayed buffer to adjust the cached
  1008.  * entries.  The entries which are inside the changed area are removed,
  1009.  * because they must be recomputed.  Entries below the changed have their line
  1010.  * number adjusted for deleted/inserted lines, and have their sst_change_lnum
  1011.  * set to indicate that a check must be made if the changed lines would change
  1012.  * the cached entry.
  1013.  *
  1014.  * When later displaying lines, an entry is stored for each line.  Displayed
  1015.  * lines are likely to be displayed again, in which case the state at the
  1016.  * start of the line is needed.
  1017.  * For not displayed lines, an entry is stored for every so many lines.  These
  1018.  * entries will be used e.g., when scrolling backwards.  The distance between
  1019.  * entries depends on the number of lines in the buffer.  For small buffers
  1020.  * the distance is fixed at SST_DIST, for large buffers there is a fixed
  1021.  * number of entries SST_MAX_ENTRIES, and the distance is computed.
  1022.  */
  1023.  
  1024. /*
  1025.  * Free b_sst_array[] for buffer "buf".
  1026.  * Used when syntax items changed to force resyncing everywhere.
  1027.  */
  1028.     void
  1029. syn_stack_free_all(buf)
  1030.     buf_T    *buf;
  1031. {
  1032.     synstate_T    *p;
  1033.     win_T    *wp;
  1034.  
  1035.     if (buf->b_sst_array != NULL)
  1036.     {
  1037.     for (p = buf->b_sst_first; p != NULL; p = p->sst_next)
  1038.         clear_syn_state(p);
  1039.     vim_free(buf->b_sst_array);
  1040.     buf->b_sst_array = NULL;
  1041.     buf->b_sst_len = 0;
  1042.     }
  1043. #ifdef FEAT_FOLDING
  1044.     /* When using "syntax" fold method, must update all folds. */
  1045.     FOR_ALL_WINDOWS(wp)
  1046.     {
  1047.     if (wp->w_buffer == buf && foldmethodIsSyntax(wp))
  1048.         foldUpdateAll(wp);
  1049.     }
  1050. #endif
  1051. }
  1052.  
  1053. /*
  1054.  * Allocate the syntax state stack for syn_buf when needed.
  1055.  * If the number of entries in b_sst_array[] is much too big or a bit too
  1056.  * small, reallocate it.
  1057.  * Also used to allocate b_sst_array[] for the first time.
  1058.  */
  1059.     static void
  1060. syn_stack_alloc()
  1061. {
  1062.     long    len;
  1063.     synstate_T    *to, *from;
  1064.     synstate_T    *sstp;
  1065.  
  1066.     len = syn_buf->b_ml.ml_line_count / SST_DIST + Rows * 2;
  1067.     if (len < SST_MIN_ENTRIES)
  1068.     len = SST_MIN_ENTRIES;
  1069.     else if (len > SST_MAX_ENTRIES)
  1070.     len = SST_MAX_ENTRIES;
  1071.     if (syn_buf->b_sst_len > len * 2 || syn_buf->b_sst_len < len)
  1072.     {
  1073.     /* Allocate 50% too much, to avoid reallocating too often. */
  1074.     len = syn_buf->b_ml.ml_line_count;
  1075.     len = (len + len / 2) / SST_DIST + Rows * 2;
  1076.     if (len < SST_MIN_ENTRIES)
  1077.         len = SST_MIN_ENTRIES;
  1078.     else if (len > SST_MAX_ENTRIES)
  1079.         len = SST_MAX_ENTRIES;
  1080.  
  1081.     if (syn_buf->b_sst_array != NULL)
  1082.     {
  1083.         /* When shrinking the array, cleanup the existing stack.
  1084.          * Make sure that all valid entries fit in the new array. */
  1085.         while (syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2> len
  1086.             && syn_stack_cleanup())
  1087.         ;
  1088.         if (len < syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2)
  1089.         len = syn_buf->b_sst_len - syn_buf->b_sst_freecount + 2;
  1090.     }
  1091.  
  1092.     sstp = (synstate_T *)alloc_clear((unsigned)(len * sizeof(synstate_T)));
  1093.     if (sstp == NULL)    /* out of memory! */
  1094.         return;
  1095.  
  1096.     to = sstp - 1;
  1097.     if (syn_buf->b_sst_array != NULL)
  1098.     {
  1099.         /* Move the states from the old array to the new one. */
  1100.         for (from = syn_buf->b_sst_first; from != NULL;
  1101.                             from = from->sst_next)
  1102.         {
  1103.         ++to;
  1104.         *to = *from;
  1105.         to->sst_next = to + 1;
  1106.         }
  1107.     }
  1108.     if (to != sstp - 1)
  1109.     {
  1110.         to->sst_next = NULL;
  1111.         syn_buf->b_sst_first = sstp;
  1112.         syn_buf->b_sst_freecount = len - (int)(to - sstp) - 1;
  1113.     }
  1114.     else
  1115.     {
  1116.         syn_buf->b_sst_first = NULL;
  1117.         syn_buf->b_sst_freecount = len;
  1118.     }
  1119.  
  1120.     /* Create the list of free entries. */
  1121.     syn_buf->b_sst_firstfree = to + 1;
  1122.     while (++to < sstp + len)
  1123.         to->sst_next = to + 1;
  1124.     (sstp + len - 1)->sst_next = NULL;
  1125.  
  1126.     vim_free(syn_buf->b_sst_array);
  1127.     syn_buf->b_sst_array = sstp;
  1128.     syn_buf->b_sst_len = len;
  1129.     }
  1130. }
  1131.  
  1132. /*
  1133.  * Check for changes in a buffer to affect stored syntax states.  Uses the
  1134.  * b_mod_* fields.
  1135.  * Called from update_screen(), before screen is being updated, once for each
  1136.  * displayed buffer.
  1137.  */
  1138.     void
  1139. syn_stack_apply_changes(buf)
  1140.     buf_T    *buf;
  1141. {
  1142.     synstate_T    *p, *prev, *np;
  1143.     linenr_T    n;
  1144.  
  1145.     if (buf->b_sst_array == NULL)    /* nothing to do */
  1146.     return;
  1147.  
  1148.     prev = NULL;
  1149.     for (p = buf->b_sst_first; p != NULL; )
  1150.     {
  1151.     if (p->sst_lnum > buf->b_mod_top)
  1152.     {
  1153.         n = p->sst_lnum + buf->b_mod_xlines;
  1154.         if (n <= buf->b_mod_bot)
  1155.         {
  1156.         /* this state is inside the changed area, remove it */
  1157.         np = p->sst_next;
  1158.         if (prev == NULL)
  1159.             buf->b_sst_first = np;
  1160.         else
  1161.             prev->sst_next = np;
  1162.         syn_stack_free_entry(buf, p);
  1163.         p = np;
  1164.         continue;
  1165.         }
  1166.         /* This state is below the changed area.  Remember the line
  1167.          * that needs to be parsed before this entry can be made valid
  1168.          * again. */
  1169.         if (p->sst_change_lnum != 0 && p->sst_change_lnum > buf->b_mod_top)
  1170.         {
  1171.         if (p->sst_change_lnum + buf->b_mod_xlines > buf->b_mod_top)
  1172.             p->sst_change_lnum += buf->b_mod_xlines;
  1173.         else
  1174.             p->sst_change_lnum = buf->b_mod_top;
  1175.         }
  1176.         if (p->sst_change_lnum == 0
  1177.             || p->sst_change_lnum < buf->b_mod_bot)
  1178.         p->sst_change_lnum = buf->b_mod_bot;
  1179.  
  1180.         p->sst_lnum = n;
  1181.     }
  1182.     prev = p;
  1183.     p = p->sst_next;
  1184.     }
  1185. }
  1186.  
  1187. /*
  1188.  * Reduce the number of entries in the state stack for syn_buf.
  1189.  * Returns TRUE if at least one entry was freed.
  1190.  */
  1191.     static int
  1192. syn_stack_cleanup()
  1193. {
  1194.     synstate_T    *p, *prev;
  1195.     disptick_T    tick;
  1196.     int        above;
  1197.     int        dist;
  1198.     int        retval = FALSE;
  1199.  
  1200.     if (syn_buf->b_sst_array == NULL || syn_buf->b_sst_first == NULL)
  1201.     return retval;
  1202.  
  1203.     /* Compute normal distance between non-displayed entries. */
  1204.     dist = syn_buf->b_ml.ml_line_count / (syn_buf->b_sst_len - Rows) + 1;
  1205.  
  1206.     /*
  1207.      * Go throught the list to find the "tick" for the oldest entry that can
  1208.      * be removed.  Set "above" when the "tick" for the oldest entry is above
  1209.      * "b_sst_lasttick" (the display tick wraps around).
  1210.      */
  1211.     tick = syn_buf->b_sst_lasttick;
  1212.     above = FALSE;
  1213.     prev = syn_buf->b_sst_first;
  1214.     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
  1215.     {
  1216.     if (prev->sst_lnum + dist > p->sst_lnum)
  1217.     {
  1218.         if (p->sst_tick > syn_buf->b_sst_lasttick)
  1219.         {
  1220.         if (!above || p->sst_tick < tick)
  1221.             tick = p->sst_tick;
  1222.         above = TRUE;
  1223.         }
  1224.         else if (!above && p->sst_tick < tick)
  1225.         tick = p->sst_tick;
  1226.     }
  1227.     }
  1228.  
  1229.     /*
  1230.      * Go through the list to make the entries for the oldest tick at an
  1231.      * interval of several lines.
  1232.      */
  1233.     prev = syn_buf->b_sst_first;
  1234.     for (p = prev->sst_next; p != NULL; prev = p, p = p->sst_next)
  1235.     {
  1236.     if (p->sst_tick == tick && prev->sst_lnum + dist > p->sst_lnum)
  1237.     {
  1238.         /* Move this entry from used list to free list */
  1239.         prev->sst_next = p->sst_next;
  1240.         syn_stack_free_entry(syn_buf, p);
  1241.         p = prev;
  1242.         retval = TRUE;
  1243.     }
  1244.     }
  1245.     return retval;
  1246. }
  1247.  
  1248. /*
  1249.  * Free the allocated memory for a syn_state item.
  1250.  * Move the entry into the free list.
  1251.  */
  1252.     static void
  1253. syn_stack_free_entry(buf, p)
  1254.     buf_T    *buf;
  1255.     synstate_T    *p;
  1256. {
  1257.     clear_syn_state(p);
  1258.     p->sst_next = buf->b_sst_firstfree;
  1259.     buf->b_sst_firstfree = p;
  1260.     ++buf->b_sst_freecount;
  1261. }
  1262.  
  1263. /*
  1264.  * Find an entry in the list of state stacks at or before "lnum".
  1265.  * Returns NULL when there is no entry or the first entry is after "lnum".
  1266.  */
  1267.     static synstate_T *
  1268. syn_stack_find_entry(lnum)
  1269.     linenr_T    lnum;
  1270. {
  1271.     synstate_T    *p, *prev;
  1272.  
  1273.     prev = NULL;
  1274.     for (p = syn_buf->b_sst_first; p != NULL; prev = p, p = p->sst_next)
  1275.     {
  1276.     if (p->sst_lnum == lnum)
  1277.         return p;
  1278.     if (p->sst_lnum > lnum)
  1279.         break;
  1280.     }
  1281.     return prev;
  1282. }
  1283.  
  1284. /*
  1285.  * Try saving the current state in b_sst_array[].
  1286.  * The current state must be valid for the start of the current_lnum line!
  1287.  */
  1288.     static synstate_T *
  1289. store_current_state(sp)
  1290.     synstate_T    *sp;    /* at or before where state is to be saved or
  1291.                    NULL */
  1292. {
  1293.     int        i;
  1294.     synstate_T    *p;
  1295.     bufstate_T    *bp;
  1296.     stateitem_T    *cur_si;
  1297.  
  1298.     if (sp == NULL)
  1299.     sp = syn_stack_find_entry(current_lnum);
  1300.  
  1301.     /*
  1302.      * If the current state contains a start or end pattern that continues
  1303.      * from the previous line, we can't use it.  Don't store it then.
  1304.      */
  1305.     for (i = current_state.ga_len - 1; i >= 0; --i)
  1306.     {
  1307.     cur_si = &CUR_STATE(i);
  1308.     if (cur_si->si_h_startpos.lnum >= current_lnum
  1309.         || cur_si->si_m_endpos.lnum >= current_lnum
  1310.         || cur_si->si_h_endpos.lnum >= current_lnum
  1311.         || (cur_si->si_end_idx
  1312.             && cur_si->si_eoe_pos.lnum >= current_lnum))
  1313.         break;
  1314.     }
  1315.     if (i >= 0)
  1316.     {
  1317.     if (sp != NULL)
  1318.     {
  1319.         /* find the entry just before this one */
  1320.         for (p = syn_buf->b_sst_first; p != NULL; p = p->sst_next)
  1321.         if (p->sst_next == sp)
  1322.             break;
  1323.         if (p != NULL)
  1324.         p->sst_next = sp->sst_next;
  1325.         else
  1326.         syn_buf->b_sst_first = sp->sst_next;
  1327.         syn_stack_free_entry(syn_buf, sp);
  1328.         sp = NULL;
  1329.     }
  1330.     }
  1331.     else if (sp == NULL || sp->sst_lnum != current_lnum)
  1332.     {
  1333.     /*
  1334.      * Add a new entry
  1335.      */
  1336.     /* If no free items, cleanup the array first. */
  1337.     if (syn_buf->b_sst_freecount == 0)
  1338.         (void)syn_stack_cleanup();
  1339.     /* Still no free items?  Must be a strange problem... */
  1340.     if (syn_buf->b_sst_freecount == 0)
  1341.         sp = NULL;
  1342.     else
  1343.     {
  1344.         /* Take the first item from the free list and put it in the used
  1345.          * list, after *sp */
  1346.         p = syn_buf->b_sst_firstfree;
  1347.         syn_buf->b_sst_firstfree = p->sst_next;
  1348.         --syn_buf->b_sst_freecount;
  1349.         if (sp == NULL)
  1350.         {
  1351.         /* Insert in front of the list */
  1352.         p->sst_next = syn_buf->b_sst_first;
  1353.         syn_buf->b_sst_first = p;
  1354.         }
  1355.         else
  1356.         {
  1357.         /* insert in list after *sp */
  1358.         p->sst_next = sp->sst_next;
  1359.         sp->sst_next = p;
  1360.         }
  1361.         sp = p;
  1362.         sp->sst_stacksize = 0;
  1363.         sp->sst_lnum = current_lnum;
  1364.     }
  1365.     }
  1366.     if (sp != NULL)
  1367.     {
  1368.     /* When overwriting an existing state stack, clear it first */
  1369.     clear_syn_state(sp);
  1370.     sp->sst_stacksize = current_state.ga_len;
  1371.     if (current_state.ga_len > SST_FIX_STATES)
  1372.     {
  1373.         /* Need to clear it, might be something remaining from when the
  1374.          * length was less than SST_FIX_STATES. */
  1375.         ga_init2(&sp->sst_union.sst_ga, (int)sizeof(bufstate_T), 1);
  1376.         if (ga_grow(&sp->sst_union.sst_ga, current_state.ga_len) == FAIL)
  1377.         sp->sst_stacksize = 0;
  1378.         else
  1379.         {
  1380.         sp->sst_union.sst_ga.ga_len = current_state.ga_len;
  1381.         sp->sst_union.sst_ga.ga_room -= current_state.ga_len;
  1382.         }
  1383.         bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
  1384.     }
  1385.     else
  1386.         bp = sp->sst_union.sst_stack;
  1387.     for (i = 0; i < sp->sst_stacksize; ++i)
  1388.     {
  1389.         bp[i].bs_idx = CUR_STATE(i).si_idx;
  1390.         bp[i].bs_flags = CUR_STATE(i).si_flags;
  1391.         bp[i].bs_extmatch = ref_extmatch(CUR_STATE(i).si_extmatch);
  1392.     }
  1393.     sp->sst_next_flags = current_next_flags;
  1394.     sp->sst_next_list = current_next_list;
  1395.     sp->sst_tick = display_tick;
  1396.     sp->sst_change_lnum = 0;
  1397.     }
  1398.     current_state_stored = TRUE;
  1399.     return sp;
  1400. }
  1401.  
  1402. /*
  1403.  * Copy a state stack from "from" in b_sst_array[] to current_state;
  1404.  */
  1405.     static void
  1406. load_current_state(from)
  1407.     synstate_T    *from;
  1408. {
  1409.     int        i;
  1410.     bufstate_T    *bp;
  1411.  
  1412.     clear_current_state();
  1413.     validate_current_state();
  1414.     keepend_level = -1;
  1415.     if (from->sst_stacksize
  1416.         && ga_grow(¤t_state, from->sst_stacksize) != FAIL)
  1417.     {
  1418.     if (from->sst_stacksize > SST_FIX_STATES)
  1419.         bp = SYN_STATE_P(&(from->sst_union.sst_ga));
  1420.     else
  1421.         bp = from->sst_union.sst_stack;
  1422.     for (i = 0; i < from->sst_stacksize; ++i)
  1423.     {
  1424.         CUR_STATE(i).si_idx = bp[i].bs_idx;
  1425.         CUR_STATE(i).si_flags = bp[i].bs_flags;
  1426.         CUR_STATE(i).si_extmatch = ref_extmatch(bp[i].bs_extmatch);
  1427.         if (keepend_level < 0 && (CUR_STATE(i).si_flags & HL_KEEPEND))
  1428.         keepend_level = i;
  1429.         CUR_STATE(i).si_ends = FALSE;
  1430.         CUR_STATE(i).si_m_lnum = 0;
  1431.         CUR_STATE(i).si_next_list =
  1432.                (SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_next_list;
  1433.         update_si_attr(i);
  1434.     }
  1435.     current_state.ga_len = from->sst_stacksize;
  1436.     current_state.ga_room -= current_state.ga_len;
  1437.     }
  1438.     current_next_list = from->sst_next_list;
  1439.     current_next_flags = from->sst_next_flags;
  1440.     current_lnum = from->sst_lnum;
  1441. }
  1442.  
  1443. /*
  1444.  * Compare saved state stack "*sp" with the current state.
  1445.  * Return TRUE when they are equal.
  1446.  */
  1447.     static int
  1448. syn_stack_equal(sp)
  1449.     synstate_T *sp;
  1450. {
  1451.     int        i, j;
  1452.     bufstate_T    *bp;
  1453.     reg_extmatch_T    *six, *bsx;
  1454.  
  1455.     /* First a quick check if the stacks have the same size end nextlist. */
  1456.     if (sp->sst_stacksize == current_state.ga_len
  1457.         && sp->sst_next_list == current_next_list)
  1458.     {
  1459.     /* Need to compare all states on both stacks. */
  1460.     if (sp->sst_stacksize > SST_FIX_STATES)
  1461.         bp = SYN_STATE_P(&(sp->sst_union.sst_ga));
  1462.     else
  1463.         bp = sp->sst_union.sst_stack;
  1464.  
  1465.     for (i = current_state.ga_len; --i >= 0; )
  1466.     {
  1467.         /* If the item has another index the state is different. */
  1468.         if (bp[i].bs_idx != CUR_STATE(i).si_idx)
  1469.         break;
  1470.         if (bp[i].bs_extmatch != CUR_STATE(i).si_extmatch)
  1471.         {
  1472.         /* When the extmatch pointers are different, the strings in
  1473.          * them can still be the same.  Check if the extmatch
  1474.          * references are equal. */
  1475.         bsx = bp[i].bs_extmatch;
  1476.         six = CUR_STATE(i).si_extmatch;
  1477.         /* If one of the extmatch pointers is NULL the states are
  1478.          * different. */
  1479.         if (bsx == NULL || six == NULL)
  1480.             break;
  1481.         for (j = 0; j < NSUBEXP; ++j)
  1482.         {
  1483.             /* Check each referenced match string. They must all be
  1484.              * equal. */
  1485.             if (bsx->matches[j] != six->matches[j])
  1486.             {
  1487.             /* If the pointer is different it can still be the
  1488.              * same text.  Compare the strings, ignore case when
  1489.              * the start item has the sp_ic flag set. */
  1490.             if (bsx->matches[j] == NULL
  1491.                 || six->matches[j] == NULL)
  1492.                 break;
  1493.             if ((SYN_ITEMS(syn_buf)[CUR_STATE(i).si_idx]).sp_ic
  1494.                 ? MB_STRICMP(bsx->matches[j],
  1495.                              six->matches[j]) != 0
  1496.                 : STRCMP(bsx->matches[j], six->matches[j]) != 0)
  1497.                 break;
  1498.             }
  1499.         }
  1500.         if (j != NSUBEXP)
  1501.             break;
  1502.         }
  1503.     }
  1504.     if (i < 0)
  1505.         return TRUE;
  1506.     }
  1507.     return FALSE;
  1508. }
  1509.  
  1510. /*
  1511.  * We stop parsing syntax above line "lnum".  If the stored state at or below
  1512.  * this line depended on a change before it, it now depends on the line below
  1513.  * the last parsed line.
  1514.  * The window looks like this:
  1515.  *        line which changed
  1516.  *        displayed line
  1517.  *        displayed line
  1518.  * lnum ->  line below window
  1519.  */
  1520.     void
  1521. syntax_end_parsing(lnum)
  1522.     linenr_T    lnum;
  1523. {
  1524.     synstate_T    *sp;
  1525.  
  1526.     sp = syn_stack_find_entry(lnum);
  1527.     if (sp != NULL && sp->sst_lnum < lnum)
  1528.     sp = sp->sst_next;
  1529.  
  1530.     if (sp != NULL && sp->sst_change_lnum != 0)
  1531.     sp->sst_change_lnum = lnum;
  1532. }
  1533.  
  1534. /*
  1535.  * End of handling of the state stack.
  1536.  ****************************************/
  1537.  
  1538.     static void
  1539. invalidate_current_state()
  1540. {
  1541.     clear_current_state();
  1542.     current_state.ga_itemsize = 0;    /* mark current_state invalid */
  1543.     current_next_list = NULL;
  1544.     keepend_level = -1;
  1545. }
  1546.  
  1547.     static void
  1548. validate_current_state()
  1549. {
  1550.     current_state.ga_itemsize = sizeof(stateitem_T);
  1551.     current_state.ga_growsize = 3;
  1552. }
  1553.  
  1554. /*
  1555.  * Return TRUE if the syntax at start of lnum changed since last time.
  1556.  * This will only be called just after get_syntax_attr() for the previous
  1557.  * line, to check if the next line needs to be redrawn too.
  1558.  */
  1559.     int
  1560. syntax_check_changed(lnum)
  1561.     linenr_T    lnum;
  1562. {
  1563.     int        retval = TRUE;
  1564.     synstate_T    *sp;
  1565.  
  1566.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  1567.  
  1568.     /*
  1569.      * Check the state stack when:
  1570.      * - lnum is just below the previously syntaxed line.
  1571.      * - lnum is not before the lines with saved states.
  1572.      * - lnum is not past the lines with saved states.
  1573.      * - lnum is at or before the last changed line.
  1574.      */
  1575.     if (VALID_STATE(¤t_state) && lnum == current_lnum + 1)
  1576.     {
  1577.     sp = syn_stack_find_entry(lnum);
  1578.     if (sp != NULL && sp->sst_lnum == lnum)
  1579.     {
  1580.         /*
  1581.          * finish the previous line (needed when not all of the line was
  1582.          * drawn)
  1583.          */
  1584.         (void)syn_finish_line(FALSE);
  1585.  
  1586.         /*
  1587.          * Compare the current state with the previously saved state of
  1588.          * the line.
  1589.          */
  1590.         if (syn_stack_equal(sp))
  1591.         retval = FALSE;
  1592.  
  1593.         /*
  1594.          * Store the current state in b_sst_array[] for later use.
  1595.          */
  1596.         ++current_lnum;
  1597.         (void)store_current_state(NULL);
  1598.     }
  1599.     }
  1600.  
  1601.     reg_syn = FALSE;
  1602.  
  1603.     return retval;
  1604. }
  1605.  
  1606. /*
  1607.  * Finish the current line.
  1608.  * This doesn't return any attributes, it only gets the state at the end of
  1609.  * the line.  It can start anywhere in the line, as long as the current state
  1610.  * is valid.
  1611.  */
  1612.     static int
  1613. syn_finish_line(syncing)
  1614.     int        syncing;        /* called for syncing */
  1615. {
  1616.     stateitem_T    *cur_si;
  1617.  
  1618.     if (!current_finished)
  1619.     {
  1620.     while (!current_finished)
  1621.     {
  1622.         (void)syn_current_attr(syncing, FALSE);
  1623.         /*
  1624.          * When syncing, and found some item, need to check the item.
  1625.          */
  1626.         if (syncing && current_state.ga_len)
  1627.         {
  1628.         /*
  1629.          * Check for match with sync item.
  1630.          */
  1631.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  1632.         if (SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags
  1633.                            & (HL_SYNC_HERE|HL_SYNC_THERE))
  1634.             return TRUE;
  1635.  
  1636.         /* syn_current_attr() will have skipped the check for an item
  1637.          * that ends here, need to do that now. */
  1638.         ++current_col;
  1639.         check_state_ends();
  1640.         --current_col;
  1641.         }
  1642.         ++current_col;
  1643.     }
  1644.     }
  1645.     return FALSE;
  1646. }
  1647.  
  1648. /*
  1649.  * Return highlight attributes for next character.
  1650.  * Must first call syntax_start() once for the line.
  1651.  * "col" is normally 0 for the first use in a line, and increments by one each
  1652.  * time.  It's allowed to skip characters and to stop before the end of the
  1653.  * line.  But only a "col" after a previously used column is allowed.
  1654.  */
  1655.     int
  1656. get_syntax_attr(col)
  1657.     colnr_T    col;
  1658. {
  1659.     int        attr = 0;
  1660.  
  1661.     /* check for out of memory situation */
  1662.     if (syn_buf->b_sst_array == NULL)
  1663.     return 0;
  1664.  
  1665.     reg_syn = TRUE;    /* let vim_regexec() know we're using syntax */
  1666.  
  1667.     /* Make sure current_state is valid */
  1668.     if (INVALID_STATE(¤t_state))
  1669.     validate_current_state();
  1670.  
  1671.     /*
  1672.      * Skip from the current column to "col", get the attributes for "col".
  1673.      */
  1674.     while (current_col <= col)
  1675.     {
  1676.     attr = syn_current_attr(FALSE, TRUE);
  1677.     ++current_col;
  1678.     }
  1679.  
  1680.     reg_syn = FALSE;
  1681.     return attr;
  1682. }
  1683.  
  1684. /*
  1685.  * Get syntax attributes for current_lnum, current_col.
  1686.  */
  1687.     static int
  1688. syn_current_attr(syncing, displaying)
  1689.     int        syncing;        /* When 1: called for syncing */
  1690.     int        displaying;        /* result will be displayed */
  1691. {
  1692.     int        syn_id;
  1693.     lpos_T    endpos;        /* was: char_u *endp; */
  1694.     lpos_T    hl_startpos;    /* was: int hl_startcol; */
  1695.     lpos_T    hl_endpos;
  1696.     lpos_T    eos_pos;    /* end-of-start match (start region) */
  1697.     lpos_T    eoe_pos;    /* end-of-end pattern */
  1698.     int        end_idx;    /* group ID for end pattern */
  1699.     int        idx;
  1700.     synpat_T    *spp;
  1701.     stateitem_T    *cur_si, *sip;
  1702.     int        startcol;
  1703.     int        endcol;
  1704.     int        flags;
  1705.     short    *next_list;
  1706.     int        found_match;            /* found usable match */
  1707.     static int    try_next_column = FALSE;    /* must try in next col */
  1708.     int        do_keywords;
  1709.     regmmatch_T    regmatch;
  1710.     lpos_T    pos;
  1711.     int        lc_col;
  1712.     reg_extmatch_T *cur_extmatch = NULL;
  1713.     char_u    *line;        /* current line.  NOTE: becomes invalid after
  1714.                    looking for a pattern match! */
  1715.     int        keep_next_list;
  1716.     int        zero_width_next_list = FALSE;
  1717.  
  1718.     /*
  1719.      * No character, no attributes!  Past end of line?
  1720.      * Do try matching with an empty line (could be the start of a region).
  1721.      */
  1722.     line = syn_getcurline();
  1723.     if (line[current_col] == NUL && current_col != 0)
  1724.     {
  1725.     /*
  1726.      * If we found a match after the last column, use it.
  1727.      */
  1728.     if (next_match_idx >= 0 && next_match_col >= (int)current_col
  1729.                           && next_match_col != MAXCOL)
  1730.         (void)push_next_match(NULL);
  1731.  
  1732.     current_finished = TRUE;
  1733.     current_state_stored = FALSE;
  1734.     return 0;
  1735.     }
  1736.  
  1737.     /* if the current or next character is NUL, we will finish the line now */
  1738.     if (line[current_col] == NUL || line[current_col + 1] == NUL)
  1739.     {
  1740.     current_finished = TRUE;
  1741.     current_state_stored = FALSE;
  1742.     }
  1743.  
  1744.     /*
  1745.      * When in the previous column there was a match but it could not be used
  1746.      * (empty match or already matched in this column) need to try again in
  1747.      * the next column.
  1748.      */
  1749.     if (try_next_column)
  1750.     {
  1751.     next_match_idx = -1;
  1752.     try_next_column = FALSE;
  1753.     }
  1754.  
  1755.     /* Only check for keywords when not syncing and there are some. */
  1756.     do_keywords = !syncing
  1757.             && (syn_buf->b_keywtab != NULL
  1758.                 || syn_buf->b_keywtab_ic != NULL);
  1759.  
  1760.     /*
  1761.      * Repeat matching keywords and patterns, to find contained items at the
  1762.      * same column.  This stops when there are no extra matches at the current
  1763.      * column.
  1764.      */
  1765.     do
  1766.     {
  1767.     found_match = FALSE;
  1768.     keep_next_list = FALSE;
  1769.     syn_id = 0;
  1770.  
  1771.     /*
  1772.      * 1. Check for a current state.
  1773.      *    Only when there is no current state, or if the current state may
  1774.      *    contain other things, we need to check for keywords and patterns.
  1775.      */
  1776.     if (current_state.ga_len)
  1777.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  1778.     else
  1779.         cur_si = NULL;
  1780.  
  1781.     if (cur_si == NULL || cur_si->si_cont_list != NULL)
  1782.     {
  1783.         /*
  1784.          * 2. Check for keywords, if on a keyword char after a non-keyword
  1785.          *      char.  Don't do this when syncing.
  1786.          */
  1787.         if (do_keywords)
  1788.         {
  1789.           line = syn_getcurline();
  1790.           if (vim_iswordc_buf(line + current_col, syn_buf)
  1791.               && (current_col == 0
  1792.               || !vim_iswordc_buf(line + current_col - 1
  1793. #ifdef FEAT_MBYTE
  1794.                   - (has_mbyte
  1795.                   ? (*mb_head_off)(line, line + current_col - 1)
  1796.                   : 0)
  1797. #endif
  1798.                    , syn_buf)))
  1799.           {
  1800.         syn_id = check_keyword_id(line, (int)current_col,
  1801.                      &endcol, &flags, &next_list, cur_si);
  1802.         if (syn_id)
  1803.         {
  1804.             if (push_current_state(KEYWORD_IDX) == OK)
  1805.             {
  1806.             cur_si = &CUR_STATE(current_state.ga_len - 1);
  1807.             cur_si->si_m_startcol = current_col;
  1808.             cur_si->si_h_startpos.lnum = current_lnum;
  1809.             cur_si->si_h_startpos.col = 0;    /* starts right away */
  1810.             cur_si->si_m_endpos.lnum = current_lnum;
  1811.             cur_si->si_m_endpos.col = endcol;
  1812.             cur_si->si_h_endpos.lnum = current_lnum;
  1813.             cur_si->si_h_endpos.col = endcol;
  1814.             cur_si->si_ends = TRUE;
  1815.             cur_si->si_end_idx = 0;
  1816.             cur_si->si_flags = flags;
  1817.             cur_si->si_id = syn_id;
  1818.             cur_si->si_trans_id = syn_id;
  1819.             if (flags & HL_TRANSP)
  1820.             {
  1821.                 if (current_state.ga_len < 2)
  1822.                 {
  1823.                 cur_si->si_attr = 0;
  1824.                 cur_si->si_trans_id = 0;
  1825.                 }
  1826.                 else
  1827.                 {
  1828.                 cur_si->si_attr = CUR_STATE(
  1829.                     current_state.ga_len - 2).si_attr;
  1830.                 cur_si->si_trans_id = CUR_STATE(
  1831.                     current_state.ga_len - 2).si_trans_id;
  1832.                 }
  1833.             }
  1834.             else
  1835.                 cur_si->si_attr = syn_id2attr(syn_id);
  1836.             cur_si->si_cont_list = NULL;
  1837.             cur_si->si_next_list = next_list;
  1838.             check_keepend();
  1839.             }
  1840.             else
  1841.             vim_free(next_list);
  1842.         }
  1843.           }
  1844.         }
  1845.  
  1846.         /*
  1847.          * 3. Check for patterns (only if not found a keyword).
  1848.          */
  1849.         if (syn_id == 0 && syn_buf->b_syn_patterns.ga_len)
  1850.         {
  1851.         /*
  1852.          * If we didn't check for a match yet, or we are past it, check
  1853.          * for any match with a pattern.
  1854.          */
  1855.         if (next_match_idx < 0 || next_match_col < (int)current_col)
  1856.         {
  1857.             /*
  1858.              * Check all relevant patterns for a match at this
  1859.              * position.  This is complicated, because matching with a
  1860.              * pattern takes quite a bit of time, thus we want to
  1861.              * avoid doing it when it's not needed.
  1862.              */
  1863.             next_match_idx = 0;        /* no match in this line yet */
  1864.             next_match_col = MAXCOL;
  1865.             for (idx = syn_buf->b_syn_patterns.ga_len; --idx >= 0; )
  1866.             {
  1867.             spp = &(SYN_ITEMS(syn_buf)[idx]);
  1868.             if (       spp->sp_syncing == syncing
  1869.                 && (displaying || !(spp->sp_flags & HL_DISPLAY))
  1870.                 && (spp->sp_type == SPTYPE_MATCH
  1871.                     || spp->sp_type == SPTYPE_START)
  1872.                 && (current_next_list != NULL
  1873.                     ? in_id_list(NULL, current_next_list,
  1874.                                   &spp->sp_syn, 0)
  1875.                     : (cur_si == NULL
  1876.                     ? !(spp->sp_flags & HL_CONTAINED)
  1877.                     : in_id_list(cur_si,
  1878.                         cur_si->si_cont_list, &spp->sp_syn,
  1879.                         spp->sp_flags & HL_CONTAINED))))
  1880.             {
  1881.                 /* If we already tried matching in this line, and
  1882.                  * there isn't a match before next_match_col, skip
  1883.                  * this item. */
  1884.                 if (spp->sp_line_id == current_line_id
  1885.                     && spp->sp_startcol >= next_match_col)
  1886.                 continue;
  1887.                 spp->sp_line_id = current_line_id;
  1888.  
  1889.                 lc_col = current_col - spp->sp_offsets[SPO_LC_OFF];
  1890.                 if (lc_col < 0)
  1891.                 lc_col = 0;
  1892.  
  1893.                 regmatch.rmm_ic = spp->sp_ic;
  1894.                 regmatch.regprog = spp->sp_prog;
  1895.                 if (!syn_regexec(®match, current_lnum,
  1896.                                  (colnr_T)lc_col))
  1897.                 {
  1898.                 /* no match in this line, try another one */
  1899.                 spp->sp_startcol = MAXCOL;
  1900.                 continue;
  1901.                 }
  1902.  
  1903.                 /*
  1904.                  * Compute the first column of the match.
  1905.                  */
  1906.                 syn_add_start_off(&pos, ®match,
  1907.                              spp, SPO_MS_OFF, -1);
  1908.                 if (pos.lnum > current_lnum)
  1909.                 {
  1910.                 /* must have used end of match in a next line,
  1911.                  * we can't handle that */
  1912.                 spp->sp_startcol = MAXCOL;
  1913.                 continue;
  1914.                 }
  1915.                 startcol = pos.col;
  1916.  
  1917.                 /* remember the next column where this pattern
  1918.                  * matches in the current line */
  1919.                 spp->sp_startcol = startcol;
  1920.  
  1921.                 /*
  1922.                  * If a previously found match starts at a lower
  1923.                  * column number, don't use this one.
  1924.                  */
  1925.                 if (startcol >= next_match_col)
  1926.                 continue;
  1927.  
  1928.                 /*
  1929.                  * If we matched this pattern at this position
  1930.                  * before, skip it.  Must retry in the next
  1931.                  * column, because it may match from there.
  1932.                  */
  1933.                 if (did_match_already(idx))
  1934.                 {
  1935.                 try_next_column = TRUE;
  1936.                 continue;
  1937.                 }
  1938.  
  1939.                 endpos.lnum = regmatch.endpos[0].lnum;
  1940.                 endpos.col = regmatch.endpos[0].col;
  1941.  
  1942.                 /* Compute the highlight start. */
  1943.                 syn_add_start_off(&hl_startpos, ®match,
  1944.                              spp, SPO_HS_OFF, -1);
  1945.  
  1946.                 /* Compute the region start. */
  1947.                 /* Default is to use the end of the match. */
  1948.                 syn_add_end_off(&eos_pos, ®match,
  1949.                              spp, SPO_RS_OFF, 0);
  1950.  
  1951.                 /*
  1952.                  * Grab the external submatches before they get
  1953.                  * overwritten.  Reference count doesn't change.
  1954.                  */
  1955.                 unref_extmatch(cur_extmatch);
  1956.                 cur_extmatch = re_extmatch_out;
  1957.                 re_extmatch_out = NULL;
  1958.  
  1959.                 flags = 0;
  1960.                 eoe_pos.lnum = 0;    /* avoid warning */
  1961.                 eoe_pos.col = 0;
  1962.                 end_idx = 0;
  1963.                 hl_endpos.lnum = 0;
  1964.  
  1965.                 /*
  1966.                  * For a "oneline" the end must be found in the
  1967.                  * same line too.  Search for it after the end of
  1968.                  * the match with the start pattern.  Set the
  1969.                  * resulting end positions at the same time.
  1970.                  */
  1971.                 if (spp->sp_type == SPTYPE_START
  1972.                           && (spp->sp_flags & HL_ONELINE))
  1973.                 {
  1974.                 lpos_T    startpos;
  1975.  
  1976.                 startpos = endpos;
  1977.                 find_endpos(idx, &startpos, &endpos, &hl_endpos,
  1978.                     &flags, &eoe_pos, &end_idx, cur_extmatch);
  1979.                 if (endpos.lnum == 0)
  1980.                     continue;        /* not found */
  1981.                 }
  1982.  
  1983.                 /*
  1984.                  * For a "match" the size must be > 0 after the
  1985.                  * end offset needs has been added.  Except when
  1986.                  * syncing.
  1987.                  */
  1988.                 else if (spp->sp_type == SPTYPE_MATCH)
  1989.                 {
  1990.                 syn_add_end_off(&hl_endpos, ®match, spp,
  1991.                                    SPO_HE_OFF, 0);
  1992.                 syn_add_end_off(&endpos, ®match, spp,
  1993.                                    SPO_ME_OFF, 0);
  1994.                 if (endpos.lnum == current_lnum
  1995.                       && (int)endpos.col + syncing < startcol)
  1996.                 {
  1997.                     /*
  1998.                      * If an empty string is matched, may need
  1999.                      * to try matching again at next column.
  2000.                      */
  2001.                     if (regmatch.startpos[0].col
  2002.                             == regmatch.endpos[0].col)
  2003.                     try_next_column = TRUE;
  2004.                     continue;
  2005.                 }
  2006.                 }
  2007.  
  2008.                 /*
  2009.                  * keep the best match so far in next_match_*
  2010.                  */
  2011.                 /* Highlighting must start after startpos and end
  2012.                  * before endpos. */
  2013.                 if (hl_startpos.lnum == current_lnum
  2014.                        && (int)hl_startpos.col < startcol)
  2015.                 hl_startpos.col = startcol;
  2016.                 limit_pos_zero(&hl_endpos, &endpos);
  2017.  
  2018.                 next_match_idx = idx;
  2019.                 next_match_col = startcol;
  2020.                 next_match_m_endpos = endpos;
  2021.                 next_match_h_endpos = hl_endpos;
  2022.                 next_match_h_startpos = hl_startpos;
  2023.                 next_match_flags = flags;
  2024.                 next_match_eos_pos = eos_pos;
  2025.                 next_match_eoe_pos = eoe_pos;
  2026.                 next_match_end_idx = end_idx;
  2027.                 unref_extmatch(next_match_extmatch);
  2028.                 next_match_extmatch = cur_extmatch;
  2029.                 cur_extmatch = NULL;
  2030.             }
  2031.             }
  2032.         }
  2033.  
  2034.         /*
  2035.          * If we found a match at the current column, use it.
  2036.          */
  2037.         if (next_match_idx >= 0 && next_match_col == (int)current_col)
  2038.         {
  2039.             synpat_T    *lspp;
  2040.  
  2041.             /* When a zero-width item matched which has a nextgroup,
  2042.              * don't push the item but set nextgroup. */
  2043.             lspp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
  2044.             if (next_match_m_endpos.lnum == current_lnum
  2045.                 && next_match_m_endpos.col == current_col
  2046.                 && lspp->sp_next_list != NULL)
  2047.             {
  2048.             current_next_list = lspp->sp_next_list;
  2049.             current_next_flags = lspp->sp_flags;
  2050.             keep_next_list = TRUE;
  2051.             zero_width_next_list = TRUE;
  2052.             next_match_idx = -1;
  2053.             }
  2054.             else
  2055.             cur_si = push_next_match(cur_si);
  2056.             found_match = TRUE;
  2057.         }
  2058.         }
  2059.     }
  2060.  
  2061.     /*
  2062.      * Handle searching for nextgroup match.
  2063.      */
  2064.     if (current_next_list != NULL && !keep_next_list)
  2065.     {
  2066.         /*
  2067.          * If a nextgroup was not found, continue looking for one if:
  2068.          * - this is an empty line and the "skipempty" option was given
  2069.          * - we are on white space and the "skipwhite" option was given
  2070.          */
  2071.         if (!found_match)
  2072.         {
  2073.         line = syn_getcurline();
  2074.         if (((current_next_flags & HL_SKIPWHITE)
  2075.                 && vim_iswhite(line[current_col]))
  2076.             || ((current_next_flags & HL_SKIPEMPTY)
  2077.                 && *line == NUL))
  2078.             break;
  2079.         }
  2080.  
  2081.         /*
  2082.          * If a nextgroup was found: Use it, and continue looking for
  2083.          * contained matches.
  2084.          * If a nextgroup was not found: Continue looking for a normal
  2085.          * match.
  2086.          * When did set current_next_list for a zero-width item and no
  2087.          * match was found don't loop (would get stuck).
  2088.          */
  2089.         current_next_list = NULL;
  2090.         next_match_idx = -1;
  2091.         if (!zero_width_next_list)
  2092.         found_match = TRUE;
  2093.     }
  2094.  
  2095.     } while (found_match);
  2096.  
  2097.     /*
  2098.      * Use attributes from the current state, if within its highlighting.
  2099.      * If not, use attributes from the current-but-one state, etc.
  2100.      */
  2101.     current_attr = 0;
  2102. #ifdef FEAT_EVAL
  2103.     current_id = 0;
  2104.     current_trans_id = 0;
  2105. #endif
  2106.     if (cur_si != NULL)
  2107.     {
  2108.     for (idx = current_state.ga_len - 1; idx >= 0; --idx)
  2109.     {
  2110.         sip = &CUR_STATE(idx);
  2111.         if ((current_lnum > sip->si_h_startpos.lnum
  2112.             || (current_lnum == sip->si_h_startpos.lnum
  2113.                 && (int)current_col >= sip->si_h_startpos.col))
  2114.             && (sip->si_h_endpos.lnum == 0
  2115.             || current_lnum < sip->si_h_endpos.lnum
  2116.             || (current_lnum == sip->si_h_endpos.lnum
  2117.                 && (int)current_col < sip->si_h_endpos.col)))
  2118.         {
  2119.         current_attr = sip->si_attr;
  2120. #ifdef FEAT_EVAL
  2121.         current_id = sip->si_id;
  2122.         current_trans_id = sip->si_trans_id;
  2123. #endif
  2124.         break;
  2125.         }
  2126.     }
  2127.  
  2128.     /*
  2129.      * Check for end of current state (and the states before it) at the
  2130.      * next column.  Don't do this for syncing, because we would miss a
  2131.      * single character match.
  2132.      * First check if the current state ends at the current column.  It
  2133.      * may be for an empty match and a containing item might end in the
  2134.      * current column.
  2135.      */
  2136.     if (!syncing)
  2137.     {
  2138.         check_state_ends();
  2139.         if (current_state.ga_len > 0)
  2140.         {
  2141.         ++current_col;
  2142.         check_state_ends();
  2143.         --current_col;
  2144.         }
  2145.     }
  2146.     }
  2147.  
  2148.     /* nextgroup ends at end of line, unless "skipnl" or "skipemtpy" present */
  2149.     if (current_next_list != NULL
  2150.         && syn_getcurline()[current_col + 1] == NUL
  2151.         && !(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY)))
  2152.     current_next_list = NULL;
  2153.  
  2154.     /* No longer need external matches.  But keep next_match_extmatch. */
  2155.     unref_extmatch(re_extmatch_out);
  2156.     re_extmatch_out = NULL;
  2157.     unref_extmatch(cur_extmatch);
  2158.  
  2159.     return current_attr;
  2160. }
  2161.  
  2162.  
  2163. /*
  2164.  * Check if we already matched pattern "idx" at the current column.
  2165.  */
  2166.     static int
  2167. did_match_already(idx)
  2168.     int        idx;
  2169. {
  2170.     int        i;
  2171.  
  2172.     for (i = current_state.ga_len; --i >= 0; )
  2173.     {
  2174.     if (CUR_STATE(i).si_m_startcol == (int)current_col
  2175.         && CUR_STATE(i).si_m_lnum == (int)current_lnum
  2176.         && CUR_STATE(i).si_idx == idx)
  2177.         return TRUE;
  2178.     }
  2179.     return FALSE;
  2180. }
  2181.  
  2182. /*
  2183.  * Push the next match onto the stack.
  2184.  */
  2185.     static stateitem_T *
  2186. push_next_match(cur_si)
  2187.     stateitem_T    *cur_si;
  2188. {
  2189.     synpat_T    *spp;
  2190.  
  2191.     spp = &(SYN_ITEMS(syn_buf)[next_match_idx]);
  2192.  
  2193.     /*
  2194.      * Push the item in current_state stack;
  2195.      */
  2196.     if (push_current_state(next_match_idx) == OK)
  2197.     {
  2198.     /*
  2199.      * If it's a start-skip-end type that crosses lines, figure out how
  2200.      * much it continues in this line.  Otherwise just fill in the length.
  2201.      */
  2202.     cur_si = &CUR_STATE(current_state.ga_len - 1);
  2203.     cur_si->si_h_startpos = next_match_h_startpos;
  2204.     cur_si->si_m_startcol = current_col;
  2205.     cur_si->si_m_lnum = current_lnum;
  2206.     cur_si->si_flags = spp->sp_flags;
  2207.     cur_si->si_next_list = spp->sp_next_list;
  2208.     cur_si->si_extmatch = ref_extmatch(next_match_extmatch);
  2209.     if (spp->sp_type == SPTYPE_START && !(spp->sp_flags & HL_ONELINE))
  2210.     {
  2211.         /* Try to find the end pattern in the current line */
  2212.         update_si_end(cur_si, (int)(next_match_m_endpos.col), TRUE);
  2213.         check_keepend();
  2214.     }
  2215.     else
  2216.     {
  2217.         cur_si->si_m_endpos = next_match_m_endpos;
  2218.         cur_si->si_h_endpos = next_match_h_endpos;
  2219.         cur_si->si_ends = TRUE;
  2220.         cur_si->si_flags |= next_match_flags;
  2221.         cur_si->si_eoe_pos = next_match_eoe_pos;
  2222.         cur_si->si_end_idx = next_match_end_idx;
  2223.     }
  2224.     if (keepend_level < 0 && (cur_si->si_flags & HL_KEEPEND))
  2225.         keepend_level = current_state.ga_len - 1;
  2226.     check_keepend();
  2227.     update_si_attr(current_state.ga_len - 1);
  2228.  
  2229.     /*
  2230.      * If the start pattern has another highlight group, push another item
  2231.      * on the stack for the start pattern.
  2232.      */
  2233.     if (       spp->sp_type == SPTYPE_START
  2234.         && spp->sp_syn_match_id != 0
  2235.         && push_current_state(next_match_idx) == OK)
  2236.     {
  2237.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  2238.         cur_si->si_h_startpos = next_match_h_startpos;
  2239.         cur_si->si_m_startcol = current_col;
  2240.         cur_si->si_m_lnum = current_lnum;
  2241.         cur_si->si_m_endpos = next_match_eos_pos;
  2242.         cur_si->si_h_endpos = next_match_eos_pos;
  2243.         cur_si->si_ends = TRUE;
  2244.         cur_si->si_end_idx = 0;
  2245.         cur_si->si_flags = HL_MATCH;
  2246.         cur_si->si_next_list = NULL;
  2247.         check_keepend();
  2248.         update_si_attr(current_state.ga_len - 1);
  2249.     }
  2250.     }
  2251.  
  2252.     next_match_idx = -1;    /* try other match next time */
  2253.  
  2254.     return cur_si;
  2255. }
  2256.  
  2257. /*
  2258.  * Check for end of current state (and the states before it).
  2259.  */
  2260.     static void
  2261. check_state_ends()
  2262. {
  2263.     stateitem_T    *cur_si;
  2264.     int        had_extend = FALSE;
  2265.  
  2266.     cur_si = &CUR_STATE(current_state.ga_len - 1);
  2267.     for (;;)
  2268.     {
  2269.     if (cur_si->si_ends
  2270.         && (cur_si->si_m_endpos.lnum < current_lnum
  2271.             || (cur_si->si_m_endpos.lnum == current_lnum
  2272.             && cur_si->si_m_endpos.col <= (int)current_col)))
  2273.     {
  2274.         /*
  2275.          * If there is an end pattern group ID, highlight the end pattern
  2276.          * now.  No need to pop the current item from the stack.
  2277.          * Only do this if the end pattern continues beyond the current
  2278.          * position.
  2279.          */
  2280.         if (cur_si->si_end_idx
  2281.             && (cur_si->si_eoe_pos.lnum > current_lnum
  2282.             || (cur_si->si_eoe_pos.lnum == current_lnum
  2283.                 && cur_si->si_eoe_pos.col > (int)current_col)))
  2284.         {
  2285.         cur_si->si_idx = cur_si->si_end_idx;
  2286.         cur_si->si_end_idx = 0;
  2287.         cur_si->si_m_endpos = cur_si->si_eoe_pos;
  2288.         cur_si->si_h_endpos = cur_si->si_eoe_pos;
  2289.         cur_si->si_flags |= HL_MATCH;
  2290.         update_si_attr(current_state.ga_len - 1);
  2291.         break;
  2292.         }
  2293.         else
  2294.         {
  2295.         /* handle next_list, unless at end of line and no "skipnl" or
  2296.          * "skipempty" */
  2297.         current_next_list = cur_si->si_next_list;
  2298.         current_next_flags = cur_si->si_flags;
  2299.         if (!(current_next_flags & (HL_SKIPNL | HL_SKIPEMPTY))
  2300.             && syn_getcurline()[current_col] == NUL)
  2301.             current_next_list = NULL;
  2302.  
  2303.         /* When the ended item has "extend", another item with
  2304.          * "keepend" now needs to check for its end. */
  2305.          if (cur_si->si_flags & HL_EXTEND)
  2306.              had_extend = TRUE;
  2307.  
  2308.         pop_current_state();
  2309.  
  2310.         if (current_state.ga_len == 0)
  2311.             break;
  2312.  
  2313.         if (had_extend)
  2314.         {
  2315.             syn_update_ends(FALSE);
  2316.             if (current_state.ga_len == 0)
  2317.             break;
  2318.         }
  2319.  
  2320.         cur_si = &CUR_STATE(current_state.ga_len - 1);
  2321.  
  2322.         /*
  2323.          * Only for a region the search for the end continues after
  2324.          * the end of the contained item.  If the contained match
  2325.          * included the end-of-line, break here, the region continues.
  2326.          * Don't do this when:
  2327.          * - "keepend" is used for the contained item
  2328.          * - not at the end of the line (could be end="x$"me=e-1).
  2329.          * - "excludenl" is used (HL_HAS_EOL won't be set)
  2330.          */
  2331.         if (SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_type == SPTYPE_START
  2332.                  && !(cur_si->si_flags & (HL_MATCH | HL_KEEPEND)))
  2333.         {
  2334.             update_si_end(cur_si, (int)current_col, TRUE);
  2335.             check_keepend();
  2336.             if ((current_next_flags & HL_HAS_EOL)
  2337.                 && keepend_level < 0
  2338.                 && syn_getcurline()[current_col] == NUL)
  2339.             break;
  2340.         }
  2341.         }
  2342.     }
  2343.     else
  2344.         break;
  2345.     }
  2346. }
  2347.  
  2348. /*
  2349.  * Update an entry in the current_state stack for a match or region.  This
  2350.  * fills in si_attr, si_next_list and si_cont_list.
  2351.  */
  2352.     static void
  2353. update_si_attr(idx)
  2354.     int        idx;
  2355. {
  2356.     stateitem_T    *sip = &CUR_STATE(idx);
  2357.     synpat_T    *spp;
  2358.  
  2359.     spp = &(SYN_ITEMS(syn_buf)[sip->si_idx]);
  2360.     if (sip->si_flags & HL_MATCH)
  2361.     sip->si_id = spp->sp_syn_match_id;
  2362.     else
  2363.     sip->si_id = spp->sp_syn.id;
  2364.     sip->si_attr = syn_id2attr(sip->si_id);
  2365.     sip->si_trans_id = sip->si_id;
  2366.     if (sip->si_flags & HL_MATCH)
  2367.     sip->si_cont_list = NULL;
  2368.     else
  2369.     sip->si_cont_list = spp->sp_cont_list;
  2370.  
  2371.     /*
  2372.      * For transparent items, take attr from outer item.
  2373.      * Also take cont_list, if there is none.
  2374.      * Don't do this for the matchgroup of a start or end pattern.
  2375.      */
  2376.     if ((spp->sp_flags & HL_TRANSP) && !(sip->si_flags & HL_MATCH))
  2377.     {
  2378.     if (idx == 0)
  2379.     {
  2380.         sip->si_attr = 0;
  2381.         sip->si_trans_id = 0;
  2382.         if (sip->si_cont_list == NULL)
  2383.         sip->si_cont_list = ID_LIST_ALL;
  2384.     }
  2385.     else
  2386.     {
  2387.         sip->si_attr = CUR_STATE(idx - 1).si_attr;
  2388.         sip->si_trans_id = CUR_STATE(idx - 1).si_trans_id;
  2389.         if (sip->si_cont_list == NULL)
  2390.         sip->si_cont_list = CUR_STATE(idx - 1).si_cont_list;
  2391.     }
  2392.     }
  2393. }
  2394.  
  2395. /*
  2396.  * Check the current stack for patterns with "keepend" flag.
  2397.  * Propagate the match-end to contained items, until a "skipend" item is found.
  2398.  */
  2399.     static void
  2400. check_keepend()
  2401. {
  2402.     int        i;
  2403.     lpos_T    maxpos;
  2404.     stateitem_T    *sip;
  2405.  
  2406.     /*
  2407.      * This check can consume a lot of time; only do it from the level where
  2408.      * there really is a keepend.
  2409.      */
  2410.     if (keepend_level < 0)
  2411.     return;
  2412.  
  2413.     /*
  2414.      * Find the last index of an "extend" item.  "keepend" items before that
  2415.      * won't do anything.  If there is no "extend" item "i" will be
  2416.      * "keepend_level" and all "keepend" items will work normally.
  2417.      */
  2418.     for (i = current_state.ga_len - 1; i > keepend_level; --i)
  2419.     if (CUR_STATE(i).si_flags & HL_EXTEND)
  2420.         break;
  2421.  
  2422.     maxpos.lnum = 0;
  2423.     for ( ; i < current_state.ga_len; ++i)
  2424.     {
  2425.     sip = &CUR_STATE(i);
  2426.     if (maxpos.lnum != 0)
  2427.     {
  2428.         limit_pos_zero(&sip->si_m_endpos, &maxpos);
  2429.         limit_pos_zero(&sip->si_h_endpos, &maxpos);
  2430.         limit_pos_zero(&sip->si_eoe_pos, &maxpos);
  2431.         sip->si_ends = TRUE;
  2432.     }
  2433.     if (sip->si_ends
  2434.         && (sip->si_flags & HL_KEEPEND)
  2435.         && (maxpos.lnum == 0
  2436.             || maxpos.lnum > sip->si_m_endpos.lnum
  2437.             || (maxpos.lnum == sip->si_m_endpos.lnum
  2438.             && maxpos.col > sip->si_m_endpos.col)))
  2439.         maxpos = sip->si_m_endpos;
  2440.     }
  2441. }
  2442.  
  2443. /*
  2444.  * Update an entry in the current_state stack for a start-skip-end pattern.
  2445.  * This finds the end of the current item, if it's in the current line.
  2446.  *
  2447.  * Return the flags for the matched END.
  2448.  */
  2449.     static void
  2450. update_si_end(sip, startcol, force)
  2451.     stateitem_T    *sip;
  2452.     int        startcol;   /* where to start searching for the end */
  2453.     int        force;        /* when TRUE overrule a previous end */
  2454. {
  2455.     lpos_T    startpos;
  2456.     lpos_T    endpos;
  2457.     lpos_T    hl_endpos;
  2458.     lpos_T    end_endpos;
  2459.     int        end_idx;
  2460.  
  2461.     /* Don't update when it's already done.  Can be a match of an end pattern
  2462.      * that started in a previous line.  Watch out: can also be a "keepend"
  2463.      * from a containing item. */
  2464.     if (!force && sip->si_m_endpos.lnum >= current_lnum)
  2465.     return;
  2466.  
  2467.     /*
  2468.      * We need to find the end of the region.  It may continue in the next
  2469.      * line.
  2470.      */
  2471.     end_idx = 0;
  2472.     startpos.lnum = current_lnum;
  2473.     startpos.col = startcol;
  2474.     find_endpos(sip->si_idx, &startpos, &endpos, &hl_endpos,
  2475.            &(sip->si_flags), &end_endpos, &end_idx, sip->si_extmatch);
  2476.  
  2477.     if (endpos.lnum == 0)
  2478.     {
  2479.     /* No end pattern matched. */
  2480.     if (SYN_ITEMS(syn_buf)[sip->si_idx].sp_flags & HL_ONELINE)
  2481.     {
  2482.         /* a "oneline" never continues in the next line */
  2483.         sip->si_ends = TRUE;
  2484.         sip->si_m_endpos.lnum = current_lnum;
  2485.         sip->si_m_endpos.col = (colnr_T)STRLEN(syn_getcurline());
  2486.     }
  2487.     else
  2488.     {
  2489.         /* continues in the next line */
  2490.         sip->si_ends = FALSE;
  2491.         sip->si_m_endpos.lnum = 0;
  2492.     }
  2493.     sip->si_h_endpos = sip->si_m_endpos;
  2494.     }
  2495.     else
  2496.     {
  2497.     /* match within this line */
  2498.     sip->si_m_endpos = endpos;
  2499.     sip->si_h_endpos = hl_endpos;
  2500.     sip->si_eoe_pos = end_endpos;
  2501.     sip->si_ends = TRUE;
  2502.     sip->si_end_idx = end_idx;
  2503.     }
  2504. }
  2505.  
  2506. /*
  2507.  * Add a new state to the current state stack.
  2508.  * It is cleared and the index set to "idx".
  2509.  * Return FAIL if it's not possible (out of memory).
  2510.  */
  2511.     static int
  2512. push_current_state(idx)
  2513.     int        idx;
  2514. {
  2515.     if (ga_grow(¤t_state, 1) == FAIL)
  2516.     return FAIL;
  2517.     vim_memset(&CUR_STATE(current_state.ga_len), 0, sizeof(stateitem_T));
  2518.     CUR_STATE(current_state.ga_len).si_idx = idx;
  2519.     ++current_state.ga_len;
  2520.     --current_state.ga_room;
  2521.     return OK;
  2522. }
  2523.  
  2524. /*
  2525.  * Remove a state from the current_state stack.
  2526.  */
  2527.     static void
  2528. pop_current_state()
  2529. {
  2530.     if (current_state.ga_len)
  2531.     {
  2532.     unref_extmatch(CUR_STATE(current_state.ga_len - 1).si_extmatch);
  2533.     --current_state.ga_len;
  2534.     ++current_state.ga_room;
  2535.     }
  2536.     /* after the end of a pattern, try matching a keyword or pattern */
  2537.     next_match_idx = -1;
  2538.  
  2539.     /* if first state with "keepend" is popped, reset keepend_level */
  2540.     if (keepend_level >= current_state.ga_len)
  2541.     keepend_level = -1;
  2542. }
  2543.  
  2544. /*
  2545.  * Find the end of a start/skip/end syntax region after "startpos".
  2546.  * Only checks one line.
  2547.  * Also handles a match item that continued from a previous line.
  2548.  * If not found, the syntax item continues in the next line.  m_endpos->lnum
  2549.  * will be 0.
  2550.  * If found, the end of the region and the end of the highlighting is
  2551.  * computed.
  2552.  */
  2553.     static void
  2554. find_endpos(idx, startpos, m_endpos, hl_endpos, flagsp, end_endpos,
  2555.                                end_idx, start_ext)
  2556.     int        idx;        /* index of the pattern */
  2557.     lpos_T    *startpos;    /* where to start looking for an END match */
  2558.     lpos_T    *m_endpos;    /* return: end of match */
  2559.     lpos_T    *hl_endpos;    /* return: end of highlighting */
  2560.     int        *flagsp;    /* return: flags of matching END */
  2561.     lpos_T    *end_endpos;    /* return: end of end pattern match */
  2562.     int        *end_idx;    /* return: group ID for end pat. match, or 0 */
  2563.     reg_extmatch_T *start_ext;    /* submatches from the start pattern */
  2564. {
  2565.     colnr_T    matchcol;
  2566.     synpat_T    *spp, *spp_skip;
  2567.     int        start_idx;
  2568.     int        best_idx;
  2569.     regmmatch_T    regmatch;
  2570.     regmmatch_T    best_regmatch;        /* startpos/endpos of best match */
  2571.     lpos_T    pos;
  2572.     char_u    *line;
  2573.     int        had_match = FALSE;
  2574.  
  2575.     /*
  2576.      * Check for being called with a START pattern.
  2577.      * Can happen with a match that continues to the next line, because it
  2578.      * contained a region.
  2579.      */
  2580.     spp = &(SYN_ITEMS(syn_buf)[idx]);
  2581.     if (spp->sp_type != SPTYPE_START)
  2582.     {
  2583.     *hl_endpos = *startpos;
  2584.     return;
  2585.     }
  2586.  
  2587.     /*
  2588.      * Find the SKIP or first END pattern after the last START pattern.
  2589.      */
  2590.     for (;;)
  2591.     {
  2592.     spp = &(SYN_ITEMS(syn_buf)[idx]);
  2593.     if (spp->sp_type != SPTYPE_START)
  2594.         break;
  2595.     ++idx;
  2596.     }
  2597.  
  2598.     /*
  2599.      *    Lookup the SKIP pattern (if present)
  2600.      */
  2601.     if (spp->sp_type == SPTYPE_SKIP)
  2602.     {
  2603.     spp_skip = spp;
  2604.     ++idx;
  2605.     }
  2606.     else
  2607.     spp_skip = NULL;
  2608.  
  2609.     /* Setup external matches for syn_regexec(). */
  2610.     unref_extmatch(re_extmatch_in);
  2611.     re_extmatch_in = ref_extmatch(start_ext);
  2612.  
  2613.     matchcol = startpos->col;    /* start looking for a match at sstart */
  2614.     start_idx = idx;        /* remember the first END pattern. */
  2615.     best_regmatch.startpos[0].col = 0;        /* avoid compiler warning */
  2616.     for (;;)
  2617.     {
  2618.     /*
  2619.      * Find end pattern that matches first after "matchcol".
  2620.      */
  2621.     best_idx = -1;
  2622.     for (idx = start_idx; idx < syn_buf->b_syn_patterns.ga_len; ++idx)
  2623.     {
  2624.         int lc_col = matchcol;
  2625.  
  2626.         spp = &(SYN_ITEMS(syn_buf)[idx]);
  2627.         if (spp->sp_type != SPTYPE_END)    /* past last END pattern */
  2628.         break;
  2629.         lc_col -= spp->sp_offsets[SPO_LC_OFF];
  2630.         if (lc_col < 0)
  2631.         lc_col = 0;
  2632.  
  2633.         regmatch.rmm_ic = spp->sp_ic;
  2634.         regmatch.regprog = spp->sp_prog;
  2635.         if (syn_regexec(®match, startpos->lnum, lc_col))
  2636.         {
  2637.         if (best_idx == -1 || regmatch.startpos[0].col
  2638.                           < best_regmatch.startpos[0].col)
  2639.         {
  2640.             best_idx = idx;
  2641.             best_regmatch.startpos[0] = regmatch.startpos[0];
  2642.             best_regmatch.endpos[0] = regmatch.endpos[0];
  2643.         }
  2644.         }
  2645.     }
  2646.  
  2647.     /*
  2648.      * If all end patterns have been tried, and there is no match, the
  2649.      * item continues until end-of-line.
  2650.      */
  2651.     if (best_idx == -1)
  2652.         break;
  2653.  
  2654.     /*
  2655.      * If the skip pattern matches before the end pattern,
  2656.      * continue searching after the skip pattern.
  2657.      */
  2658.     if (spp_skip != NULL)
  2659.     {
  2660.         int lc_col = matchcol - spp_skip->sp_offsets[SPO_LC_OFF];
  2661.  
  2662.         if (lc_col < 0)
  2663.         lc_col = 0;
  2664.         regmatch.rmm_ic = spp_skip->sp_ic;
  2665.         regmatch.regprog = spp_skip->sp_prog;
  2666.         if (syn_regexec(®match, startpos->lnum, lc_col)
  2667.             && regmatch.startpos[0].col
  2668.                          <= best_regmatch.startpos[0].col)
  2669.         {
  2670.         /* Add offset to skip pattern match */
  2671.         syn_add_end_off(&pos, ®match, spp_skip, SPO_ME_OFF, 1);
  2672.  
  2673.         /* If the skip pattern goes on to the next line, there is no
  2674.          * match with an end pattern in this line. */
  2675.         if (pos.lnum > startpos->lnum)
  2676.             break;
  2677.  
  2678.         line = ml_get_buf(syn_buf, startpos->lnum, FALSE);
  2679.  
  2680.         /* take care of an empty match or negative offset */
  2681.         if (pos.col <= matchcol)
  2682.             ++matchcol;
  2683.         else if (pos.col <= regmatch.endpos[0].col)
  2684.             matchcol = pos.col;
  2685.         else
  2686.             /* Be careful not to jump over the NUL at the end-of-line */
  2687.             for (matchcol = regmatch.endpos[0].col;
  2688.                 line[matchcol] != NUL && matchcol < pos.col;
  2689.                                    ++matchcol)
  2690.             ;
  2691.  
  2692.         /* if the skip pattern includes end-of-line, break here */
  2693.         if (line[matchcol] == NUL)
  2694.             break;
  2695.  
  2696.         continue;        /* start with first end pattern again */
  2697.         }
  2698.     }
  2699.  
  2700.     /*
  2701.      * Match from start pattern to end pattern.
  2702.      * Correct for match and highlight offset of end pattern.
  2703.      */
  2704.     spp = &(SYN_ITEMS(syn_buf)[best_idx]);
  2705.     syn_add_end_off(m_endpos, &best_regmatch, spp, SPO_ME_OFF, 1);
  2706.     /* can't end before the start */
  2707.     if (m_endpos->lnum == startpos->lnum && m_endpos->col < startpos->col)
  2708.         m_endpos->col = startpos->col;
  2709.  
  2710.     syn_add_end_off(end_endpos, &best_regmatch, spp, SPO_HE_OFF, 1);
  2711.     /* can't end before the start */
  2712.     if (end_endpos->lnum == startpos->lnum
  2713.                        && end_endpos->col < startpos->col)
  2714.         end_endpos->col = startpos->col;
  2715.     /* can't end after the match */
  2716.     limit_pos(end_endpos, m_endpos);
  2717.  
  2718.     /*
  2719.      * If the end group is highlighted differently, adjust the pointers.
  2720.      */
  2721.     if (spp->sp_syn_match_id != spp->sp_syn.id && spp->sp_syn_match_id != 0)
  2722.     {
  2723.         *end_idx = best_idx;
  2724.         if (spp->sp_off_flags & (1 << (SPO_RE_OFF + SPO_COUNT)))
  2725.         {
  2726.         hl_endpos->lnum = best_regmatch.endpos[0].lnum;
  2727.         hl_endpos->col = best_regmatch.endpos[0].col;
  2728.         }
  2729.         else
  2730.         {
  2731.         hl_endpos->lnum = best_regmatch.startpos[0].lnum;
  2732.         hl_endpos->col = best_regmatch.startpos[0].col;
  2733.         }
  2734.         hl_endpos->col += spp->sp_offsets[SPO_RE_OFF];
  2735.  
  2736.         /* can't end before the start */
  2737.         if (hl_endpos->lnum == startpos->lnum
  2738.                         && hl_endpos->col < startpos->col)
  2739.         hl_endpos->col = startpos->col;
  2740.         limit_pos(hl_endpos, m_endpos);
  2741.  
  2742.         /* now the match ends where the highlighting ends (why?) */
  2743.         m_endpos->col = hl_endpos->col;
  2744.     }
  2745.     else
  2746.     {
  2747.         *end_idx = 0;
  2748.         *hl_endpos = *end_endpos;
  2749.     }
  2750.  
  2751.     *flagsp = spp->sp_flags;
  2752.  
  2753.     had_match = TRUE;
  2754.     break;
  2755.     }
  2756.  
  2757.     /* no match for an END pattern in this line */
  2758.     if (!had_match)
  2759.     m_endpos->lnum = 0;
  2760.  
  2761.     /* Remove external matches. */
  2762.     unref_extmatch(re_extmatch_in);
  2763.     re_extmatch_in = NULL;
  2764. }
  2765.  
  2766. /*
  2767.  * Limit "pos" not to be after "limit".
  2768.  */
  2769.     static void
  2770. limit_pos(pos, limit)
  2771.     lpos_T    *pos;
  2772.     lpos_T    *limit;
  2773. {
  2774.     if (pos->lnum > limit->lnum)
  2775.     *pos = *limit;
  2776.     else if (pos->lnum == limit->lnum && pos->col > limit->col)
  2777.     pos->col = limit->col;
  2778. }
  2779.  
  2780. /*
  2781.  * Limit "pos" not to be after "limit", unless pos->lnum is zero.
  2782.  */
  2783.     static void
  2784. limit_pos_zero(pos, limit)
  2785.     lpos_T    *pos;
  2786.     lpos_T    *limit;
  2787. {
  2788.     if (pos->lnum == 0)
  2789.     *pos = *limit;
  2790.     else
  2791.     limit_pos(pos, limit);
  2792. }
  2793.  
  2794. /*
  2795.  * Add offset to matched text for end of match or highlight.
  2796.  */
  2797.     static void
  2798. syn_add_end_off(result, regmatch, spp, idx, extra)
  2799.     lpos_T    *result;    /* returned position */
  2800.     regmmatch_T    *regmatch;    /* start/end of match */
  2801.     synpat_T    *spp;        /* matched pattern */
  2802.     int        idx;        /* index of offset */
  2803.     int        extra;        /* extra chars for offset to start */
  2804. {
  2805.     int        col;
  2806.  
  2807.     if (spp->sp_off_flags & (1 << idx))
  2808.     {
  2809.     result->lnum = regmatch->startpos[0].lnum;
  2810.     col = regmatch->startpos[0].col + extra;
  2811.     }
  2812.     else
  2813.     {
  2814.     result->lnum = regmatch->endpos[0].lnum;
  2815.     col = regmatch->endpos[0].col;
  2816.     }
  2817.     col += spp->sp_offsets[idx];
  2818.     if (col < 0)
  2819.     result->col = 0;
  2820.     else
  2821.     result->col = col;
  2822. }
  2823.  
  2824. /*
  2825.  * Add offset to matched text for start of match or highlight.
  2826.  * Avoid resulting column to become negative.
  2827.  */
  2828.     static void
  2829. syn_add_start_off(result, regmatch, spp, idx, extra)
  2830.     lpos_T    *result;    /* returned position */
  2831.     regmmatch_T    *regmatch;    /* start/end of match */
  2832.     synpat_T    *spp;
  2833.     int        idx;
  2834.     int        extra;        /* extra chars for offset to end */
  2835. {
  2836.     int        col;
  2837.  
  2838.     if (spp->sp_off_flags & (1 << (idx + SPO_COUNT)))
  2839.     {
  2840.     result->lnum = regmatch->endpos[0].lnum;
  2841.     col = regmatch->endpos[0].col + extra;
  2842.     }
  2843.     else
  2844.     {
  2845.     result->lnum = regmatch->startpos[0].lnum;
  2846.     col = regmatch->startpos[0].col;
  2847.     }
  2848.     col += spp->sp_offsets[idx];
  2849.     if (col < 0)
  2850.     result->col = 0;
  2851.     else
  2852.     result->col = col;
  2853. }
  2854.  
  2855. /*
  2856.  * Get current line in syntax buffer.
  2857.  */
  2858.     static char_u *
  2859. syn_getcurline()
  2860. {
  2861.     return ml_get_buf(syn_buf, current_lnum, FALSE);
  2862. }
  2863.  
  2864. /*
  2865.  * Call vim_regexec() to match in syn_buf.
  2866.  * Returns TRUE when there is a match.
  2867.  */
  2868.     static int
  2869. syn_regexec(rmp, lnum, col)
  2870.     regmmatch_T    *rmp;
  2871.     linenr_T    lnum;
  2872.     colnr_T    col;
  2873. {
  2874.     if (vim_regexec_multi(rmp, syn_win, syn_buf, lnum, col) > 0)
  2875.     {
  2876.     rmp->startpos[0].lnum += lnum;
  2877.     rmp->endpos[0].lnum += lnum;
  2878.     return TRUE;
  2879.     }
  2880.     return FALSE;
  2881. }
  2882.  
  2883. /*
  2884.  * Check one position in a line for a matching keyword.
  2885.  * The caller must check if a keyword can start at startcol.
  2886.  * Return it's ID if found, 0 otherwise.
  2887.  */
  2888.     static int
  2889. check_keyword_id(line, startcol, endcol, flags, next_list, cur_si)
  2890.     char_u    *line;
  2891.     int        startcol;    /* position in line to check for keyword */
  2892.     int        *endcol;    /* character after found keyword */
  2893.     int        *flags;        /* flags of matching keyword */
  2894.     short    **next_list;    /* next_list of matching keyword */
  2895.     stateitem_T    *cur_si;    /* item at the top of the stack */
  2896. {
  2897.     keyentry_T    *ktab;
  2898.     char_u    *p;
  2899.     int        round;
  2900.     int        len;
  2901.     char_u    keyword[MAXKEYWLEN + 1]; /* assume max. keyword len is 80 */
  2902.  
  2903.     /* Find first character after the keyword.  First character was already
  2904.      * checked. */
  2905.     p = line + startcol;
  2906.     len = 0;
  2907.     do
  2908.     {
  2909. #ifdef FEAT_MBYTE
  2910.     if (has_mbyte)
  2911.         len += (*mb_ptr2len_check)(p + len);
  2912.     else
  2913. #endif
  2914.         ++len;
  2915.     }
  2916.     while (vim_iswordc_buf(p + len, syn_buf));
  2917.  
  2918.     if (len > MAXKEYWLEN)
  2919.     return 0;
  2920.  
  2921.     /*
  2922.      * Must make a copy of the keyword, so we can add a NUL and make it
  2923.      * lowercase.
  2924.      */
  2925.     STRNCPY(keyword, p, len);
  2926.     keyword[len] = NUL;
  2927.  
  2928.     /*
  2929.      * Try twice:
  2930.      * 1. matching case
  2931.      * 2. ignoring case
  2932.      */
  2933.     for (round = 1; round <= 2; ++round)
  2934.     {
  2935.     if ((round == 1 ? syn_buf->b_keywtab : syn_buf->b_keywtab_ic) == NULL)
  2936.         continue;
  2937.     if (round == 1)    /* match case */
  2938.         ktab = syn_buf->b_keywtab[syn_khash(keyword)];
  2939.     else /* round == 2, ignore case */
  2940.     {
  2941.         str_foldcase(keyword);
  2942.         ktab = syn_buf->b_keywtab_ic[syn_khash(keyword)];
  2943.     }
  2944.  
  2945.     /*
  2946.      * Find keywords that match.
  2947.      * When current_next_list is non-zero accept only that group, otherwise:
  2948.      *  Accept a not-contained keyword at toplevel.
  2949.      *  Accept a keyword at other levels only if it is in the contains list.
  2950.      */
  2951.     for ( ; ktab != NULL; ktab = ktab->next)
  2952.         if (   STRCMP(keyword, ktab->keyword) == 0
  2953.         && (current_next_list != 0
  2954.             ? in_id_list(NULL, current_next_list, &ktab->k_syn, 0)
  2955.             : (cur_si == NULL
  2956.             ? !(ktab->flags & HL_CONTAINED)
  2957.             : in_id_list(cur_si, cur_si->si_cont_list,
  2958.                   &ktab->k_syn, ktab->flags & HL_CONTAINED))))
  2959.         {
  2960.         *endcol = startcol + len;
  2961.         *flags = ktab->flags;
  2962.         *next_list = ktab->next_list;
  2963.         return ktab->k_syn.id;
  2964.         }
  2965.     }
  2966.     return 0;
  2967. }
  2968.  
  2969. /*
  2970.  * Handle ":syntax case" command.
  2971.  */
  2972. /* ARGSUSED */
  2973.     static void
  2974. syn_cmd_case(eap, syncing)
  2975.     exarg_T    *eap;
  2976.     int        syncing;        /* not used */
  2977. {
  2978.     char_u    *arg = eap->arg;
  2979.     char_u    *next;
  2980.  
  2981.     eap->nextcmd = find_nextcmd(arg);
  2982.     if (eap->skip)
  2983.     return;
  2984.  
  2985.     next = skiptowhite(arg);
  2986.     if (STRNICMP(arg, "match", 5) == 0 && next - arg == 5)
  2987.     curbuf->b_syn_ic = FALSE;
  2988.     else if (STRNICMP(arg, "ignore", 6) == 0 && next - arg == 6)
  2989.     curbuf->b_syn_ic = TRUE;
  2990.     else
  2991.     EMSG2(_("E390: Illegal argument: %s"), arg);
  2992. }
  2993.  
  2994. /*
  2995.  * Clear all syntax info for one buffer.
  2996.  */
  2997.     void
  2998. syntax_clear(buf)
  2999.     buf_T    *buf;
  3000. {
  3001.     int i;
  3002.  
  3003.     curbuf->b_syn_ic = FALSE;        /* Use case, by default */
  3004.  
  3005.     /* free the keywords */
  3006.     free_keywtab(buf->b_keywtab);
  3007.     buf->b_keywtab = NULL;
  3008.     free_keywtab(buf->b_keywtab_ic);
  3009.     buf->b_keywtab_ic = NULL;
  3010.  
  3011.     /* free the syntax patterns */
  3012.     for (i = buf->b_syn_patterns.ga_len; --i >= 0; )
  3013.     syn_clear_pattern(buf, i);
  3014.     ga_clear(&buf->b_syn_patterns);
  3015.  
  3016.     /* free the syntax clusters */
  3017.     for (i = buf->b_syn_clusters.ga_len; --i >= 0; )
  3018.     syn_clear_cluster(buf, i);
  3019.     ga_clear(&buf->b_syn_clusters);
  3020.  
  3021.     buf->b_syn_sync_flags = 0;
  3022.     buf->b_syn_sync_minlines = 0;
  3023.     buf->b_syn_sync_maxlines = 0;
  3024.  
  3025.     vim_free(buf->b_syn_linecont_prog);
  3026.     buf->b_syn_linecont_prog = NULL;
  3027.     vim_free(buf->b_syn_linecont_pat);
  3028.     buf->b_syn_linecont_pat = NULL;
  3029. #ifdef FEAT_FOLDING
  3030.     buf->b_syn_folditems = 0;
  3031. #endif
  3032.  
  3033.     /* free the stored states */
  3034.     syn_stack_free_all(buf);
  3035.     invalidate_current_state();
  3036. }
  3037.  
  3038. /*
  3039.  * Clear syncing info for one buffer.
  3040.  */
  3041.     static void
  3042. syntax_sync_clear()
  3043. {
  3044.     int i;
  3045.  
  3046.     /* free the syntax patterns */
  3047.     for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
  3048.     if (SYN_ITEMS(curbuf)[i].sp_syncing)
  3049.         syn_remove_pattern(curbuf, i);
  3050.  
  3051.     curbuf->b_syn_sync_flags = 0;
  3052.     curbuf->b_syn_sync_minlines = 0;
  3053.     curbuf->b_syn_sync_maxlines = 0;
  3054.  
  3055.     vim_free(curbuf->b_syn_linecont_prog);
  3056.     curbuf->b_syn_linecont_prog = NULL;
  3057.     vim_free(curbuf->b_syn_linecont_pat);
  3058.     curbuf->b_syn_linecont_pat = NULL;
  3059.  
  3060.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  3061. }
  3062.  
  3063. /*
  3064.  * Remove one pattern from the buffer's pattern list.
  3065.  */
  3066.     static void
  3067. syn_remove_pattern(buf, idx)
  3068.     buf_T    *buf;
  3069.     int        idx;
  3070. {
  3071.     synpat_T    *spp;
  3072.  
  3073.     spp = &(SYN_ITEMS(buf)[idx]);
  3074. #ifdef FEAT_FOLDING
  3075.     if (spp->sp_flags & HL_FOLD)
  3076.     --buf->b_syn_folditems;
  3077. #endif
  3078.     syn_clear_pattern(buf, idx);
  3079.     mch_memmove(spp, spp + 1,
  3080.            sizeof(synpat_T) * (buf->b_syn_patterns.ga_len - idx - 1));
  3081.     --buf->b_syn_patterns.ga_len;
  3082.     --buf->b_syn_patterns.ga_room;
  3083. }
  3084.  
  3085. /*
  3086.  * Clear and free one syntax pattern.  When clearing all, must be called from
  3087.  * last to first!
  3088.  */
  3089.     static void
  3090. syn_clear_pattern(buf, i)
  3091.     buf_T    *buf;
  3092.     int        i;
  3093. {
  3094.     vim_free(SYN_ITEMS(buf)[i].sp_pattern);
  3095.     vim_free(SYN_ITEMS(buf)[i].sp_prog);
  3096.     /* Only free sp_cont_list and sp_next_list of first start pattern */
  3097.     if (i == 0 || SYN_ITEMS(buf)[i - 1].sp_type != SPTYPE_START)
  3098.     {
  3099.     vim_free(SYN_ITEMS(buf)[i].sp_cont_list);
  3100.     vim_free(SYN_ITEMS(buf)[i].sp_next_list);
  3101.     }
  3102. }
  3103.  
  3104. /*
  3105.  * Clear and free one syntax cluster.
  3106.  */
  3107.     static void
  3108. syn_clear_cluster(buf, i)
  3109.     buf_T    *buf;
  3110.     int        i;
  3111. {
  3112.     vim_free(SYN_CLSTR(buf)[i].scl_name);
  3113.     vim_free(SYN_CLSTR(buf)[i].scl_name_u);
  3114.     vim_free(SYN_CLSTR(buf)[i].scl_list);
  3115. }
  3116.  
  3117. /*
  3118.  * Handle ":syntax clear" command.
  3119.  */
  3120.     static void
  3121. syn_cmd_clear(eap, syncing)
  3122.     exarg_T    *eap;
  3123.     int        syncing;
  3124. {
  3125.     char_u    *arg = eap->arg;
  3126.     char_u    *arg_end;
  3127.     int        id;
  3128.  
  3129.     eap->nextcmd = find_nextcmd(arg);
  3130.     if (eap->skip)
  3131.     return;
  3132.  
  3133.     /*
  3134.      * We have to disable this within ":syn include @group filename",
  3135.      * because otherwise @group would get deleted.
  3136.      * Only required for Vim 5.x syntax files, 6.0 ones don't contain ":syn
  3137.      * clear".
  3138.      */
  3139.     if (curbuf->b_syn_topgrp != 0)
  3140.     return;
  3141.  
  3142.     if (ends_excmd(*arg))
  3143.     {
  3144.     /*
  3145.      * No argument: Clear all syntax items.
  3146.      */
  3147.     if (syncing)
  3148.         syntax_sync_clear();
  3149.     else
  3150.     {
  3151.         syntax_clear(curbuf);
  3152.         do_unlet((char_u *)"b:current_syntax");
  3153.     }
  3154.     }
  3155.     else
  3156.     {
  3157.     /*
  3158.      * Clear the group IDs that are in the argument.
  3159.      */
  3160.     while (!ends_excmd(*arg))
  3161.     {
  3162.         arg_end = skiptowhite(arg);
  3163.         if (*arg == '@')
  3164.         {
  3165.         id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
  3166.         if (id == 0)
  3167.         {
  3168.             EMSG2(_("E391: No such syntax cluster: %s"), arg);
  3169.             break;
  3170.         }
  3171.         else
  3172.         {
  3173.             /*
  3174.              * We can't physically delete a cluster without changing
  3175.              * the IDs of other clusters, so we do the next best thing
  3176.              * and make it empty.
  3177.              */
  3178.             short scl_id = id - SYNID_CLUSTER;
  3179.  
  3180.             vim_free(SYN_CLSTR(curbuf)[scl_id].scl_list);
  3181.             SYN_CLSTR(curbuf)[scl_id].scl_list = NULL;
  3182.         }
  3183.         }
  3184.         else
  3185.         {
  3186.         id = syn_namen2id(arg, (int)(arg_end - arg));
  3187.         if (id == 0)
  3188.         {
  3189.             EMSG2(_(e_nogroup), arg);
  3190.             break;
  3191.         }
  3192.         else
  3193.             syn_clear_one(id, syncing);
  3194.         }
  3195.         arg = skipwhite(arg_end);
  3196.     }
  3197.     }
  3198.     redraw_curbuf_later(NOT_VALID);
  3199.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  3200. }
  3201.  
  3202. /*
  3203.  * Clear one syntax group for the current buffer.
  3204.  */
  3205.     static void
  3206. syn_clear_one(id, syncing)
  3207.     int        id;
  3208.     int        syncing;
  3209. {
  3210.     synpat_T    *spp;
  3211.     int        idx;
  3212.  
  3213.     /* Clear keywords only when not ":syn sync clear group-name" */
  3214.     if (!syncing)
  3215.     {
  3216.     (void)syn_clear_keyword(id, curbuf->b_keywtab);
  3217.     (void)syn_clear_keyword(id, curbuf->b_keywtab_ic);
  3218.     }
  3219.  
  3220.     /* clear the patterns for "id" */
  3221.     for (idx = curbuf->b_syn_patterns.ga_len; --idx >= 0; )
  3222.     {
  3223.     spp = &(SYN_ITEMS(curbuf)[idx]);
  3224.     if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
  3225.         continue;
  3226.     syn_remove_pattern(curbuf, idx);
  3227.     }
  3228. }
  3229.  
  3230. /*
  3231.  * Handle ":syntax on" command.
  3232.  */
  3233. /* ARGSUSED */
  3234.     static void
  3235. syn_cmd_on(eap, syncing)
  3236.     exarg_T    *eap;
  3237.     int        syncing;    /* not used */
  3238. {
  3239.     syn_cmd_onoff(eap, "syntax");
  3240. }
  3241.  
  3242. /*
  3243.  * Handle ":syntax enable" command.
  3244.  */
  3245. /* ARGSUSED */
  3246.     static void
  3247. syn_cmd_enable(eap, syncing)
  3248.     exarg_T    *eap;
  3249.     int        syncing;    /* not used */
  3250. {
  3251.     set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"enable");
  3252.     syn_cmd_onoff(eap, "syntax");
  3253.     do_unlet((char_u *)"g:syntax_cmd");
  3254. }
  3255.  
  3256. /*
  3257.  * Handle ":syntax reset" command.
  3258.  */
  3259. /* ARGSUSED */
  3260.     static void
  3261. syn_cmd_reset(eap, syncing)
  3262.     exarg_T    *eap;
  3263.     int        syncing;    /* not used */
  3264. {
  3265.     eap->nextcmd = check_nextcmd(eap->arg);
  3266.     if (!eap->skip)
  3267.     {
  3268.     set_internal_string_var((char_u *)"syntax_cmd", (char_u *)"reset");
  3269.     do_cmdline_cmd((char_u *)"runtime! syntax/syncolor.vim");
  3270.     do_unlet((char_u *)"g:syntax_cmd");
  3271.     }
  3272. }
  3273.  
  3274. /*
  3275.  * Handle ":syntax manual" command.
  3276.  */
  3277. /* ARGSUSED */
  3278.     static void
  3279. syn_cmd_manual(eap, syncing)
  3280.     exarg_T    *eap;
  3281.     int        syncing;    /* not used */
  3282. {
  3283.     syn_cmd_onoff(eap, "manual");
  3284. }
  3285.  
  3286. /*
  3287.  * Handle ":syntax off" command.
  3288.  */
  3289. /* ARGSUSED */
  3290.     static void
  3291. syn_cmd_off(eap, syncing)
  3292.     exarg_T    *eap;
  3293.     int        syncing;    /* not used */
  3294. {
  3295.     syn_cmd_onoff(eap, "nosyntax");
  3296. }
  3297.  
  3298.     static void
  3299. syn_cmd_onoff(eap, name)
  3300.     exarg_T    *eap;
  3301.     char    *name;
  3302. {
  3303.     char_u    buf[100];
  3304.  
  3305.     eap->nextcmd = check_nextcmd(eap->arg);
  3306.     if (!eap->skip)
  3307.     {
  3308.     STRCPY(buf, "so ");
  3309.     sprintf((char *)buf + 3, SYNTAX_FNAME, name);
  3310.     do_cmdline_cmd(buf);
  3311.     }
  3312. }
  3313.  
  3314. /*
  3315.  * Handle ":syntax [list]" command: list current syntax words.
  3316.  */
  3317.     static void
  3318. syn_cmd_list(eap, syncing)
  3319.     exarg_T    *eap;
  3320.     int        syncing;        /* when TRUE: list syncing items */
  3321. {
  3322.     char_u    *arg = eap->arg;
  3323.     int        id;
  3324.     char_u    *arg_end;
  3325.  
  3326.     eap->nextcmd = find_nextcmd(arg);
  3327.     if (eap->skip)
  3328.     return;
  3329.  
  3330.     if (!syntax_present(curbuf))
  3331.     {
  3332.     MSG(_("No Syntax items defined for this buffer"));
  3333.     return;
  3334.     }
  3335.  
  3336.     if (syncing)
  3337.     {
  3338.     if (curbuf->b_syn_sync_flags & SF_CCOMMENT)
  3339.     {
  3340.         MSG_PUTS(_("syncing on C-style comments"));
  3341.         if (curbuf->b_syn_sync_minlines || curbuf->b_syn_sync_maxlines)
  3342.         syn_lines_msg();
  3343.         return;
  3344.     }
  3345.     else if (!(curbuf->b_syn_sync_flags & SF_MATCH))
  3346.     {
  3347.         if (curbuf->b_syn_sync_minlines == 0)
  3348.         MSG_PUTS(_("no syncing"));
  3349.         else
  3350.         {
  3351.         MSG_PUTS(_("syncing starts "));
  3352.         msg_outnum(curbuf->b_syn_sync_minlines);
  3353.         MSG_PUTS(_(" lines before top line"));
  3354.         }
  3355.         return;
  3356.     }
  3357.     MSG_PUTS_TITLE(_("\n--- Syntax sync items ---"));
  3358.     if (curbuf->b_syn_sync_minlines || curbuf->b_syn_sync_maxlines)
  3359.     {
  3360.         MSG_PUTS(_("\nsyncing on items"));
  3361.         syn_lines_msg();
  3362.     }
  3363.     }
  3364.     else
  3365.     MSG_PUTS_TITLE(_("\n--- Syntax items ---"));
  3366.     if (ends_excmd(*arg))
  3367.     {
  3368.     /*
  3369.      * No argument: List all group IDs and all syntax clusters.
  3370.      */
  3371.     for (id = 1; id <= highlight_ga.ga_len && !got_int; ++id)
  3372.         syn_list_one(id, syncing, FALSE);
  3373.     for (id = 0; id < curbuf->b_syn_clusters.ga_len && !got_int; ++id)
  3374.         syn_list_cluster(id);
  3375.     }
  3376.     else
  3377.     {
  3378.     /*
  3379.      * List the group IDs and syntax clusters that are in the argument.
  3380.      */
  3381.     while (!ends_excmd(*arg) && !got_int)
  3382.     {
  3383.         arg_end = skiptowhite(arg);
  3384.         if (*arg == '@')
  3385.         {
  3386.         id = syn_scl_namen2id(arg + 1, (int)(arg_end - arg - 1));
  3387.         if (id == 0)
  3388.             EMSG2(_("E392: No such syntax cluster: %s"), arg);
  3389.         else
  3390.             syn_list_cluster(id - SYNID_CLUSTER);
  3391.         }
  3392.         else
  3393.         {
  3394.         id = syn_namen2id(arg, (int)(arg_end - arg));
  3395.         if (id == 0)
  3396.             EMSG2(_(e_nogroup), arg);
  3397.         else
  3398.             syn_list_one(id, syncing, TRUE);
  3399.         }
  3400.         arg = skipwhite(arg_end);
  3401.     }
  3402.     }
  3403.     eap->nextcmd = check_nextcmd(arg);
  3404. }
  3405.  
  3406.     static void
  3407. syn_lines_msg()
  3408. {
  3409.     MSG_PUTS("; ");
  3410.     if (curbuf->b_syn_sync_minlines)
  3411.     {
  3412.     MSG_PUTS(_("minimal "));
  3413.     msg_outnum(curbuf->b_syn_sync_minlines);
  3414.     if (curbuf->b_syn_sync_maxlines)
  3415.         MSG_PUTS(", ");
  3416.     }
  3417.     if (curbuf->b_syn_sync_maxlines)
  3418.     {
  3419.     MSG_PUTS(_("maximal "));
  3420.     msg_outnum(curbuf->b_syn_sync_maxlines);
  3421.     }
  3422.     MSG_PUTS(_(" lines before top line"));
  3423. }
  3424.  
  3425. static int  last_matchgroup;
  3426.  
  3427. struct name_list
  3428. {
  3429.     int        flag;
  3430.     char    *name;
  3431. };
  3432.  
  3433. static void syn_list_flags __ARGS((struct name_list *nl, int flags, int attr));
  3434.  
  3435. /*
  3436.  * List one syntax item, for ":syntax" or "syntax list syntax_name".
  3437.  */
  3438.     static void
  3439. syn_list_one(id, syncing, link_only)
  3440.     int        id;
  3441.     int        syncing;        /* when TRUE: list syncing items */
  3442.     int        link_only;        /* when TRUE; list link-only too */
  3443. {
  3444.     int        attr;
  3445.     int        idx;
  3446.     int        did_header = FALSE;
  3447.     synpat_T    *spp;
  3448.     static struct name_list namelist1[] =
  3449.         {
  3450.             {HL_DISPLAY, "display"},
  3451.             {HL_CONTAINED, "contained"},
  3452.             {HL_ONELINE, "oneline"},
  3453.             {HL_KEEPEND, "keepend"},
  3454.             {HL_EXTEND, "extend"},
  3455.             {HL_EXCLUDENL, "excludenl"},
  3456.             {HL_TRANSP, "transparent"},
  3457.             {HL_FOLD, "fold"},
  3458.             {0, NULL}
  3459.         };
  3460.     static struct name_list namelist2[] =
  3461.         {
  3462.             {HL_SKIPWHITE, "skipwhite"},
  3463.             {HL_SKIPNL, "skipnl"},
  3464.             {HL_SKIPEMPTY, "skipempty"},
  3465.             {0, NULL}
  3466.         };
  3467.  
  3468.     attr = hl_attr(HLF_D);        /* highlight like directories */
  3469.  
  3470.     /* list the keywords for "id" */
  3471.     if (!syncing)
  3472.     {
  3473.     did_header = syn_list_keywords(id, curbuf->b_keywtab, FALSE, attr);
  3474.     did_header = syn_list_keywords(id, curbuf->b_keywtab_ic,
  3475.                                 did_header, attr);
  3476.     }
  3477.  
  3478.     /* list the patterns for "id" */
  3479.     for (idx = 0; idx < curbuf->b_syn_patterns.ga_len && !got_int; ++idx)
  3480.     {
  3481.     spp = &(SYN_ITEMS(curbuf)[idx]);
  3482.     if (spp->sp_syn.id != id || spp->sp_syncing != syncing)
  3483.         continue;
  3484.  
  3485.     (void)syn_list_header(did_header, 999, id);
  3486.     did_header = TRUE;
  3487.     last_matchgroup = 0;
  3488.     if (spp->sp_type == SPTYPE_MATCH)
  3489.     {
  3490.         put_pattern("match", ' ', spp, attr);
  3491.         msg_putchar(' ');
  3492.     }
  3493.     else if (spp->sp_type == SPTYPE_START)
  3494.     {
  3495.         while (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_START)
  3496.         put_pattern("start", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3497.         if (SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_SKIP)
  3498.         put_pattern("skip", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3499.         while (idx < curbuf->b_syn_patterns.ga_len
  3500.                   && SYN_ITEMS(curbuf)[idx].sp_type == SPTYPE_END)
  3501.         put_pattern("end", '=', &SYN_ITEMS(curbuf)[idx++], attr);
  3502.         --idx;
  3503.         msg_putchar(' ');
  3504.     }
  3505.     syn_list_flags(namelist1, spp->sp_flags, attr);
  3506.  
  3507.     if (spp->sp_cont_list != NULL)
  3508.         put_id_list((char_u *)"contains", spp->sp_cont_list, attr);
  3509.  
  3510.     if (spp->sp_syn.cont_in_list != NULL)
  3511.         put_id_list((char_u *)"containedin",
  3512.                           spp->sp_syn.cont_in_list, attr);
  3513.  
  3514.     if (spp->sp_next_list != NULL)
  3515.     {
  3516.         put_id_list((char_u *)"nextgroup", spp->sp_next_list, attr);
  3517.         syn_list_flags(namelist2, spp->sp_flags, attr);
  3518.     }
  3519.     if (spp->sp_flags & (HL_SYNC_HERE|HL_SYNC_THERE))
  3520.     {
  3521.         if (spp->sp_flags & HL_SYNC_HERE)
  3522.         msg_puts_attr((char_u *)"grouphere", attr);
  3523.         else
  3524.         msg_puts_attr((char_u *)"groupthere", attr);
  3525.         msg_putchar(' ');
  3526.         if (spp->sp_sync_idx >= 0)
  3527.         msg_outtrans(HL_TABLE()[SYN_ITEMS(curbuf)
  3528.                    [spp->sp_sync_idx].sp_syn.id - 1].sg_name);
  3529.         else
  3530.         MSG_PUTS("NONE");
  3531.         msg_putchar(' ');
  3532.     }
  3533.     }
  3534.  
  3535.     /* list the link, if there is one */
  3536.     if (HL_TABLE()[id - 1].sg_link && (did_header || link_only) && !got_int)
  3537.     {
  3538.     (void)syn_list_header(did_header, 999, id);
  3539.     msg_puts_attr((char_u *)"links to", attr);
  3540.     msg_putchar(' ');
  3541.     msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
  3542.     }
  3543. }
  3544.  
  3545.     static void
  3546. syn_list_flags(nl, flags, attr)
  3547.     struct name_list    *nl;
  3548.     int            flags;
  3549.     int            attr;
  3550. {
  3551.     int        i;
  3552.  
  3553.     for (i = 0; nl[i].flag != 0; ++i)
  3554.     if (flags & nl[i].flag)
  3555.     {
  3556.         msg_puts_attr((char_u *)nl[i].name, attr);
  3557.         msg_putchar(' ');
  3558.     }
  3559. }
  3560.  
  3561. /*
  3562.  * List one syntax cluster, for ":syntax" or "syntax list syntax_name".
  3563.  */
  3564.     static void
  3565. syn_list_cluster(id)
  3566.     int id;
  3567. {
  3568.     int        endcol = 15;
  3569.  
  3570.     /* slight hack:  roughly duplicate the guts of syn_list_header() */
  3571.     msg_putchar('\n');
  3572.     msg_outtrans(SYN_CLSTR(curbuf)[id].scl_name);
  3573.  
  3574.     if (msg_col >= endcol)    /* output at least one space */
  3575.     endcol = msg_col + 1;
  3576.     if (Columns <= endcol)    /* avoid hang for tiny window */
  3577.     endcol = Columns - 1;
  3578.  
  3579.     msg_advance(endcol);
  3580.     if (SYN_CLSTR(curbuf)[id].scl_list != NULL)
  3581.     {
  3582.     put_id_list((char_u *)"cluster", SYN_CLSTR(curbuf)[id].scl_list,
  3583.             hl_attr(HLF_D));
  3584.     }
  3585.     else
  3586.     {
  3587.     msg_puts_attr((char_u *)"cluster", hl_attr(HLF_D));
  3588.     msg_puts((char_u *)"=NONE");
  3589.     }
  3590. }
  3591.  
  3592.     static void
  3593. put_id_list(name, list, attr)
  3594.     char_u    *name;
  3595.     short    *list;
  3596.     int        attr;
  3597. {
  3598.     short        *p;
  3599.  
  3600.     msg_puts_attr(name, attr);
  3601.     msg_putchar('=');
  3602.     for (p = list; *p; ++p)
  3603.     {
  3604.     if (*p >= SYNID_ALLBUT && *p < SYNID_TOP)
  3605.     {
  3606.         if (p[1])
  3607.         MSG_PUTS("ALLBUT");
  3608.         else
  3609.         MSG_PUTS("ALL");
  3610.     }
  3611.     else if (*p >= SYNID_TOP && *p < SYNID_CONTAINED)
  3612.     {
  3613.         MSG_PUTS("TOP");
  3614.     }
  3615.     else if (*p >= SYNID_CONTAINED && *p < SYNID_CLUSTER)
  3616.     {
  3617.         MSG_PUTS("CONTAINED");
  3618.     }
  3619.     else if (*p >= SYNID_CLUSTER)
  3620.     {
  3621.         short scl_id = *p - SYNID_CLUSTER;
  3622.  
  3623.         msg_putchar('@');
  3624.         msg_outtrans(SYN_CLSTR(curbuf)[scl_id].scl_name);
  3625.     }
  3626.     else
  3627.         msg_outtrans(HL_TABLE()[*p - 1].sg_name);
  3628.     if (p[1])
  3629.         msg_putchar(',');
  3630.     }
  3631.     msg_putchar(' ');
  3632. }
  3633.  
  3634.     static void
  3635. put_pattern(s, c, spp, attr)
  3636.     char    *s;
  3637.     int        c;
  3638.     synpat_T    *spp;
  3639.     int        attr;
  3640. {
  3641.     long    n;
  3642.     int        mask;
  3643.     int        first;
  3644.     static char    *sepchars = "/+=-#@\"|'^&";
  3645.     int        i;
  3646.  
  3647.     /* May have to write "matchgroup=group" */
  3648.     if (last_matchgroup != spp->sp_syn_match_id)
  3649.     {
  3650.     last_matchgroup = spp->sp_syn_match_id;
  3651.     msg_puts_attr((char_u *)"matchgroup", attr);
  3652.     msg_putchar('=');
  3653.     if (last_matchgroup == 0)
  3654.         msg_outtrans((char_u *)"NONE");
  3655.     else
  3656.         msg_outtrans(HL_TABLE()[last_matchgroup - 1].sg_name);
  3657.     msg_putchar(' ');
  3658.     }
  3659.  
  3660.     /* output the name of the pattern and an '=' or ' ' */
  3661.     msg_puts_attr((char_u *)s, attr);
  3662.     msg_putchar(c);
  3663.  
  3664.     /* output the pattern, in between a char that is not in the pattern */
  3665.     for (i = 0; vim_strchr(spp->sp_pattern, sepchars[i]) != NULL; )
  3666.     if (sepchars[++i] == NUL)
  3667.     {
  3668.         i = 0;    /* no good char found, just use the first one */
  3669.         break;
  3670.     }
  3671.     msg_putchar(sepchars[i]);
  3672.     msg_outtrans(spp->sp_pattern);
  3673.     msg_putchar(sepchars[i]);
  3674.  
  3675.     /* output any pattern options */
  3676.     first = TRUE;
  3677.     for (i = 0; i < SPO_COUNT; ++i)
  3678.     {
  3679.     mask = (1 << i);
  3680.     if (spp->sp_off_flags & (mask + (mask << SPO_COUNT)))
  3681.     {
  3682.         if (!first)
  3683.         msg_putchar(',');    /* separate with commas */
  3684.         msg_puts((char_u *)spo_name_tab[i]);
  3685.         n = spp->sp_offsets[i];
  3686.         if (i != SPO_LC_OFF)
  3687.         {
  3688.         if (spp->sp_off_flags & mask)
  3689.             msg_putchar('s');
  3690.         else
  3691.             msg_putchar('e');
  3692.         if (n > 0)
  3693.             msg_putchar('+');
  3694.         }
  3695.         if (n || i == SPO_LC_OFF)
  3696.         msg_outnum(n);
  3697.         first = FALSE;
  3698.     }
  3699.     }
  3700.     msg_putchar(' ');
  3701. }
  3702.  
  3703. /*
  3704.  * List or clear the keywords for one syntax group.
  3705.  * Return TRUE if the header has been printed.
  3706.  */
  3707.     static int
  3708. syn_list_keywords(id, ktabp, did_header, attr)
  3709.     int        id;
  3710.     keyentry_T    **ktabp;
  3711.     int        did_header;        /* header has already been printed */
  3712.     int        attr;
  3713. {
  3714.     int        i;
  3715.     int        outlen;
  3716.     keyentry_T    *ktab;
  3717.     int        prev_contained = 0;
  3718.     short    *prev_next_list = NULL;
  3719.     short    *prev_cont_in_list = NULL;
  3720.     int        prev_skipnl = 0;
  3721.     int        prev_skipwhite = 0;
  3722.     int        prev_skipempty = 0;
  3723.  
  3724.     if (ktabp == NULL)
  3725.     return did_header;
  3726.  
  3727.     /*
  3728.      * Unfortunately, this list of keywords is not sorted on alphabet but on
  3729.      * hash value...
  3730.      */
  3731.     for (i = 0; i < KHASH_SIZE; ++i)
  3732.     {
  3733.     for (ktab = ktabp[i]; ktab != NULL && !got_int; ktab = ktab->next)
  3734.     {
  3735.         if (ktab->k_syn.id == id)
  3736.         {
  3737.         if (prev_contained != (ktab->flags & HL_CONTAINED)
  3738.             || prev_skipnl != (ktab->flags & HL_SKIPNL)
  3739.             || prev_skipwhite != (ktab->flags & HL_SKIPWHITE)
  3740.             || prev_skipempty != (ktab->flags & HL_SKIPEMPTY)
  3741.             || prev_cont_in_list != ktab->k_syn.cont_in_list
  3742.             || prev_next_list != ktab->next_list)
  3743.             outlen = 9999;
  3744.         else
  3745.             outlen = (int)STRLEN(ktab->keyword);
  3746.         /* output "contained" and "nextgroup" on each line */
  3747.         if (syn_list_header(did_header, outlen, id))
  3748.         {
  3749.             prev_contained = 0;
  3750.             prev_next_list = NULL;
  3751.             prev_cont_in_list = NULL;
  3752.             prev_skipnl = 0;
  3753.             prev_skipwhite = 0;
  3754.             prev_skipempty = 0;
  3755.         }
  3756.         did_header = TRUE;
  3757.         if (prev_contained != (ktab->flags & HL_CONTAINED))
  3758.         {
  3759.             msg_puts_attr((char_u *)"contained", attr);
  3760.             msg_putchar(' ');
  3761.             prev_contained = (ktab->flags & HL_CONTAINED);
  3762.         }
  3763.         if (ktab->k_syn.cont_in_list != prev_cont_in_list)
  3764.         {
  3765.             put_id_list((char_u *)"containedin",
  3766.                           ktab->k_syn.cont_in_list, attr);
  3767.             msg_putchar(' ');
  3768.             prev_cont_in_list = ktab->k_syn.cont_in_list;
  3769.         }
  3770.         if (ktab->next_list != prev_next_list)
  3771.         {
  3772.             put_id_list((char_u *)"nextgroup", ktab->next_list, attr);
  3773.             msg_putchar(' ');
  3774.             prev_next_list = ktab->next_list;
  3775.             if (ktab->flags & HL_SKIPNL)
  3776.             {
  3777.             msg_puts_attr((char_u *)"skipnl", attr);
  3778.             msg_putchar(' ');
  3779.             prev_skipnl = (ktab->flags & HL_SKIPNL);
  3780.             }
  3781.             if (ktab->flags & HL_SKIPWHITE)
  3782.             {
  3783.             msg_puts_attr((char_u *)"skipwhite", attr);
  3784.             msg_putchar(' ');
  3785.             prev_skipwhite = (ktab->flags & HL_SKIPWHITE);
  3786.             }
  3787.             if (ktab->flags & HL_SKIPEMPTY)
  3788.             {
  3789.             msg_puts_attr((char_u *)"skipempty", attr);
  3790.             msg_putchar(' ');
  3791.             prev_skipempty = (ktab->flags & HL_SKIPEMPTY);
  3792.             }
  3793.         }
  3794.         msg_outtrans(ktab->keyword);
  3795.         }
  3796.     }
  3797.     }
  3798.  
  3799.     return did_header;
  3800. }
  3801.  
  3802.     static void
  3803. syn_clear_keyword(id, ktabp)
  3804.     int        id;
  3805.     keyentry_T    **ktabp;
  3806. {
  3807.     int        i;
  3808.     keyentry_T    *ktab;
  3809.     keyentry_T    *ktab_prev;
  3810.     keyentry_T    *ktab_next;
  3811.  
  3812.     if (ktabp == NULL)        /* no keywords present */
  3813.     return;
  3814.  
  3815.     for (i = 0; i < KHASH_SIZE; ++i)
  3816.     {
  3817.     ktab_prev = NULL;
  3818.     for (ktab = ktabp[i]; ktab != NULL; )
  3819.     {
  3820.         if (ktab->k_syn.id == id)
  3821.         {
  3822.         ktab_next = ktab->next;
  3823.         if (ktab_prev == NULL)
  3824.             ktabp[i] = ktab_next;
  3825.         else
  3826.             ktab_prev->next = ktab_next;
  3827.         vim_free(ktab);
  3828.         ktab = ktab_next;
  3829.         }
  3830.         else
  3831.         {
  3832.         ktab_prev = ktab;
  3833.         ktab = ktab->next;
  3834.         }
  3835.     }
  3836.     }
  3837. }
  3838.  
  3839. /*
  3840.  * Recursive function to free() a branch of a kwordtab.
  3841.  */
  3842.     static void
  3843. free_keywtab(ktabp)
  3844.     keyentry_T    **ktabp;
  3845. {
  3846.     int        i;
  3847.     keyentry_T    *ktab;
  3848.     keyentry_T    *ktab_next;
  3849.  
  3850.     if (ktabp != NULL)
  3851.     {
  3852.     for (i = 0; i < KHASH_SIZE; ++i)
  3853.         for (ktab = ktabp[i]; ktab != NULL; ktab = ktab_next)
  3854.         {
  3855.         ktab_next = ktab->next;
  3856.         vim_free(ktab->next_list);
  3857.         vim_free(ktab->k_syn.cont_in_list);
  3858.         vim_free(ktab);
  3859.         }
  3860.     vim_free(ktabp);
  3861.     }
  3862. }
  3863.  
  3864. /*
  3865.  * Add a keyword to the list of keywords.
  3866.  */
  3867.     static void
  3868. add_keyword(name, id, flags, cont_in_list, next_list)
  3869.     char_u    *name;        /* name of keyword */
  3870.     int        id;        /* group ID for this keyword */
  3871.     int        flags;        /* flags for this keyword */
  3872.     short    *cont_in_list; /* containedin for this keyword */
  3873.     short    *next_list; /* nextgroup for this keyword */
  3874. {
  3875.     keyentry_T    *ktab;
  3876.     keyentry_T    ***ktabpp;
  3877.     int        hash;
  3878.  
  3879.     ktab = (keyentry_T *)alloc((int)(sizeof(keyentry_T) + STRLEN(name)));
  3880.     if (ktab == NULL)
  3881.     return;
  3882.     STRCPY(ktab->keyword, name);
  3883.     ktab->k_syn.id = id;
  3884.     ktab->k_syn.inc_tag = current_syn_inc_tag;
  3885.     ktab->flags = flags;
  3886.     ktab->k_syn.cont_in_list = copy_id_list(cont_in_list);
  3887.     ktab->next_list = copy_id_list(next_list);
  3888.  
  3889.     if (curbuf->b_syn_ic)
  3890.     {
  3891.     str_foldcase(ktab->keyword);
  3892.     ktabpp = &curbuf->b_keywtab_ic;
  3893.     }
  3894.     else
  3895.     ktabpp = &curbuf->b_keywtab;
  3896.  
  3897.     if (*ktabpp == NULL)
  3898.     {
  3899.     *ktabpp = (keyentry_T **)alloc_clear(
  3900.                     (int)(sizeof(keyentry_T *) * KHASH_SIZE));
  3901.     if (*ktabpp == NULL)
  3902.         return;
  3903.     }
  3904.  
  3905.     hash = syn_khash(ktab->keyword);
  3906.     ktab->next = (*ktabpp)[hash];
  3907.     (*ktabpp)[hash] = ktab;
  3908. }
  3909.  
  3910. /*
  3911.  * Compute a hash value for a keyword.  Uses the ElfHash algorithm, which is
  3912.  * supposed to have an even distribution (suggested by Charles Campbell).
  3913.  */
  3914.     static int
  3915. syn_khash(p)
  3916.     char_u    *p;
  3917. {
  3918.     long_u    hash = 0;
  3919.     long_u    g;
  3920.  
  3921.     while (*p != NUL)
  3922.     {
  3923.     hash = (hash << 4) + *p++;    /* clear low 4 bits of hash, add char */
  3924.     g = hash & 0xf0000000L;        /* g has high 4 bits of hash only */
  3925.     if (g != 0)
  3926.         hash ^= g >> 24;        /* xor g's high 4 bits into hash */
  3927.     }
  3928.  
  3929.     return (int)(hash & KHASH_MASK);
  3930. }
  3931.  
  3932. /*
  3933.  * Get the start and end of the group name argument.
  3934.  * Return a pointer to the first argument.
  3935.  * Return NULL if the end of the command was found instead of further args.
  3936.  */
  3937.     static char_u *
  3938. get_group_name(arg, name_end)
  3939.     char_u    *arg;        /* start of the argument */
  3940.     char_u    **name_end;    /* pointer to end of the name */
  3941. {
  3942.     char_u    *rest;
  3943.  
  3944.     *name_end = skiptowhite(arg);
  3945.     rest = skipwhite(*name_end);
  3946.  
  3947.     /*
  3948.      * Check if there are enough arguments.  The first argument may be a
  3949.      * pattern, where '|' is allowed, so only check for NUL.
  3950.      */
  3951.     if (ends_excmd(*arg) || *rest == NUL)
  3952.     return NULL;
  3953.     return rest;
  3954. }
  3955.  
  3956. /*
  3957.  * Check for syntax command option arguments.
  3958.  * This can be called at any place in the list of arguments, and just picks
  3959.  * out the arguments that are known.  Can be called several times in a row to
  3960.  * collect all options in between other arguments.
  3961.  * Return a pointer to the next argument (which isn't an option).
  3962.  * Return NULL for any error;
  3963.  */
  3964.     static char_u *
  3965. get_syn_options(arg, flagsp, keyword, sync_idx, cont_list,
  3966.                               cont_in_list, next_list)
  3967.     char_u    *arg;        /* next argument */
  3968.     int        *flagsp;    /* flags for contained and transpartent */
  3969.     int        keyword;    /* TRUE for ":syn keyword" */
  3970.     int        *sync_idx;    /* syntax item for "grouphere" argument, NULL
  3971.                    if not allowed */
  3972.     short    **cont_list;    /* group IDs for "contains" argument, NULL if
  3973.                    not allowed */
  3974.     short    **cont_in_list;    /* group IDs for "containedin" argument, NULL
  3975.                    if not allowed */
  3976.     short    **next_list;    /* group IDs for "nextgroup" argument */
  3977. {
  3978.     int        flags;
  3979.     char_u    *gname_start, *gname;
  3980.     int        syn_id;
  3981.     int        len;
  3982.     int        i;
  3983.     int        fidx;
  3984.     static struct flag
  3985.     {
  3986.     char    *name;
  3987.     int    len;
  3988.     int    val;
  3989.     } flagtab[] = { {"contained",   9,    HL_CONTAINED},
  3990.             {"oneline",        7,    HL_ONELINE},
  3991.             {"keepend",        7,    HL_KEEPEND},
  3992.             {"extend",        6,    HL_EXTEND},
  3993.             {"excludenl",   9,    HL_EXCLUDENL},
  3994.             {"transparent", 11, HL_TRANSP},
  3995.             {"skipnl",        6,    HL_SKIPNL},
  3996.             {"skipwhite",   9,    HL_SKIPWHITE},
  3997.             {"skipempty",   9,    HL_SKIPEMPTY},
  3998.             {"grouphere",   9,    HL_SYNC_HERE},
  3999.             {"groupthere",  10,    HL_SYNC_THERE},
  4000.             {"display",        7,    HL_DISPLAY},
  4001.             {"fold",        4,    HL_FOLD},
  4002.         };
  4003. #define MLEN 12
  4004.     char    lowname[MLEN];
  4005.     int        llen;
  4006.  
  4007.     if (arg == NULL)        /* already detected error */
  4008.     return NULL;
  4009.  
  4010.     flags = *flagsp;
  4011.     for (;;)
  4012.     {
  4013.     /* STRNICMP() is a bit slow, change arg to lowercase first and use
  4014.      * STRNCMP() */
  4015.     for (llen = 0; llen < MLEN; ++llen)
  4016.     {
  4017.         if (!isalpha(arg[llen]))
  4018.         break;
  4019.         lowname[llen] = TO_LOWER(arg[llen]);
  4020.     }
  4021.  
  4022.     for (fidx = sizeof(flagtab) / sizeof(struct flag); --fidx >= 0; )
  4023.     {
  4024.         len = flagtab[fidx].len;
  4025.         if (len == llen
  4026.             && STRNCMP(lowname, flagtab[fidx].name, len) == 0
  4027.             && (ends_excmd(arg[len]) || vim_iswhite(arg[len])))
  4028.         {
  4029.         if (keyword
  4030.             && (flagtab[fidx].val == HL_DISPLAY
  4031.                 || flagtab[fidx].val == HL_FOLD
  4032.                 || flagtab[fidx].val == HL_EXTEND))
  4033.         {
  4034.             /* treat "display", "fold" and "extend" as a keyword */
  4035.             fidx = -1;
  4036.             break;
  4037.         }
  4038.  
  4039.         flags |= flagtab[fidx].val;
  4040.         arg = skipwhite(arg + len);
  4041.  
  4042.         if (flagtab[fidx].val == HL_SYNC_HERE
  4043.             || flagtab[fidx].val == HL_SYNC_THERE)
  4044.         {
  4045.             if (sync_idx == NULL)
  4046.             {
  4047.             EMSG(_("E393: group[t]here not accepted here"));
  4048.             return NULL;
  4049.             }
  4050.             gname_start = arg;
  4051.             arg = skiptowhite(arg);
  4052.             if (gname_start == arg)
  4053.             return NULL;
  4054.             gname = vim_strnsave(gname_start, (int)(arg - gname_start));
  4055.             if (gname == NULL)
  4056.             return NULL;
  4057.             if (STRCMP(gname, "NONE") == 0)
  4058.             *sync_idx = NONE_IDX;
  4059.             else
  4060.             {
  4061.             syn_id = syn_name2id(gname);
  4062.             for (i = curbuf->b_syn_patterns.ga_len; --i >= 0; )
  4063.                 if (SYN_ITEMS(curbuf)[i].sp_syn.id == syn_id
  4064.                 && SYN_ITEMS(curbuf)[i].sp_type == SPTYPE_START)
  4065.                 {
  4066.                 *sync_idx = i;
  4067.                 break;
  4068.                 }
  4069.             if (i < 0)
  4070.             {
  4071.                 EMSG2(_("E394: Didn't find region item for %s"), gname);
  4072.                 vim_free(gname);
  4073.                 return NULL;
  4074.             }
  4075.             }
  4076.  
  4077.             vim_free(gname);
  4078.             arg = skipwhite(arg);
  4079.         }
  4080. #ifdef FEAT_FOLDING
  4081.         else if (flagtab[fidx].val == HL_FOLD
  4082.                         && foldmethodIsSyntax(curwin))
  4083.         {
  4084.             /* Need to update folds later. */
  4085.             foldUpdateAll(curwin);
  4086.         }
  4087. #endif
  4088.         break;
  4089.         }
  4090.     }
  4091.     if (fidx >= 0)
  4092.         continue;
  4093.  
  4094.     if (llen == 8 && STRNCMP(lowname, "contains", 8) == 0
  4095.         && (vim_iswhite(arg[8]) || arg[8] == '='))
  4096.     {
  4097.         if (cont_list == NULL)
  4098.         {
  4099.         EMSG(_("E395: contains argument not accepted here"));
  4100.         return NULL;
  4101.         }
  4102.         if (get_id_list(&arg, 8, cont_list) == FAIL)
  4103.         return NULL;
  4104.     }
  4105.     else if (llen == 11 && STRNCMP(lowname, "containedin", 11) == 0
  4106.         && (vim_iswhite(arg[11]) || arg[11] == '='))
  4107.     {
  4108.         if (cont_in_list == NULL)
  4109.         {
  4110.         EMSG(_("E396: containedin argument not accepted here"));
  4111.         return NULL;
  4112.         }
  4113.         if (get_id_list(&arg, 11, cont_in_list) == FAIL)
  4114.         return NULL;
  4115.     }
  4116.     else if (llen == 9 && STRNCMP(lowname, "nextgroup", 9) == 0
  4117.         && (vim_iswhite(arg[9]) || arg[9] == '='))
  4118.     {
  4119.         if (get_id_list(&arg, 9, next_list) == FAIL)
  4120.         return NULL;
  4121.     }
  4122.     else
  4123.         break;
  4124.     }
  4125.  
  4126.     *flagsp = flags;
  4127.  
  4128.     return arg;
  4129. }
  4130.  
  4131. /*
  4132.  * Adjustments to syntax item when declared in a ":syn include"'d file.
  4133.  * Set the contained flag, and if the item is not already contained, add it
  4134.  * to the specified top-level group, if any.
  4135.  */
  4136.     static void
  4137. syn_incl_toplevel(id, flagsp)
  4138.     int        id;
  4139.     int        *flagsp;
  4140. {
  4141.     if ((*flagsp & HL_CONTAINED) || curbuf->b_syn_topgrp == 0)
  4142.     return;
  4143.     *flagsp |= HL_CONTAINED;
  4144.     if (curbuf->b_syn_topgrp >= SYNID_CLUSTER)
  4145.     {
  4146.     /* We have to alloc this, because syn_combine_list() will free it. */
  4147.     short        *grp_list = (short *)alloc((unsigned)(2 * sizeof(short)));
  4148.     int        tlg_id = curbuf->b_syn_topgrp - SYNID_CLUSTER;
  4149.  
  4150.     if (grp_list != NULL)
  4151.     {
  4152.         grp_list[0] = id;
  4153.         grp_list[1] = 0;
  4154.         syn_combine_list(&SYN_CLSTR(curbuf)[tlg_id].scl_list, &grp_list,
  4155.              CLUSTER_ADD);
  4156.     }
  4157.     }
  4158. }
  4159.  
  4160. /*
  4161.  * Handle ":syntax include [@{group-name}] filename" command.
  4162.  */
  4163. /* ARGSUSED */
  4164.     static void
  4165. syn_cmd_include(eap, syncing)
  4166.     exarg_T    *eap;
  4167.     int        syncing;        /* not used */
  4168. {
  4169.     char_u    *arg = eap->arg;
  4170.     int        sgl_id = 1;
  4171.     char_u    *group_name_end;
  4172.     char_u    *rest;
  4173.     char_u    *errormsg = NULL;
  4174.     int        prev_toplvl_grp;
  4175.     int        prev_syn_inc_tag;
  4176.     int        source = FALSE;
  4177.  
  4178.     eap->nextcmd = find_nextcmd(arg);
  4179.     if (eap->skip)
  4180.     return;
  4181.  
  4182.     if (arg[0] == '@')
  4183.     {
  4184.     ++arg;
  4185.     rest = get_group_name(arg, &group_name_end);
  4186.     if (rest == NULL)
  4187.     {
  4188.         EMSG((char_u *)_("E397: Filename required"));
  4189.         return;
  4190.     }
  4191.     sgl_id = syn_check_cluster(arg, (int)(group_name_end - arg));
  4192.     /* separate_nextcmd() and expand_filename() depend on this */
  4193.     eap->arg = rest;
  4194.     }
  4195.  
  4196.     /*
  4197.      * Everything that's left, up to the next command, should be the
  4198.      * filename to include.
  4199.      */
  4200.     eap->argt |= (XFILE | NOSPC);
  4201.     separate_nextcmd(eap);
  4202.     if (*eap->arg == '<' || *eap->arg == '$' || mch_isFullName(eap->arg))
  4203.     {
  4204.     /* For an absolute path, "$VIM/..." or "<sfile>.." we ":source" the
  4205.      * file.  Need to expand the file name first.  In other cases
  4206.      * ":runtime!" is used. */
  4207.     source = TRUE;
  4208.     if (expand_filename(eap, syn_cmdlinep, &errormsg) == FAIL)
  4209.     {
  4210.         if (errormsg != NULL)
  4211.         EMSG(errormsg);
  4212.         return;
  4213.     }
  4214.     }
  4215.  
  4216.     /*
  4217.      * Save and restore the existing top-level grouplist id and ":syn
  4218.      * include" tag around the actual inclusion.
  4219.      */
  4220.     prev_syn_inc_tag = current_syn_inc_tag;
  4221.     current_syn_inc_tag = ++running_syn_inc_tag;
  4222.     prev_toplvl_grp = curbuf->b_syn_topgrp;
  4223.     curbuf->b_syn_topgrp = sgl_id;
  4224.     if (source ? do_source(eap->arg, FALSE, FALSE) == FAIL
  4225.                     : cmd_runtime(eap->arg, TRUE) == FAIL)
  4226.     EMSG2(_(e_notopen), eap->arg);
  4227.     curbuf->b_syn_topgrp = prev_toplvl_grp;
  4228.     current_syn_inc_tag = prev_syn_inc_tag;
  4229. }
  4230.  
  4231. /*
  4232.  * Handle ":syntax keyword {group-name} [{option}] keyword .." command.
  4233.  */
  4234. /* ARGSUSED */
  4235.     static void
  4236. syn_cmd_keyword(eap, syncing)
  4237.     exarg_T    *eap;
  4238.     int        syncing;        /* not used */
  4239. {
  4240.     char_u    *arg = eap->arg;
  4241.     char_u    *group_name_end;
  4242.     int        syn_id;
  4243.     char_u    *rest;
  4244.     char_u    *keyword_copy;
  4245.     char_u    *p;
  4246.     char_u    *first_arg;
  4247.     int        round;
  4248.     int        flags = 0;
  4249.     short    *next_list = NULL;
  4250.     short    *cont_in_list = NULL;
  4251.  
  4252.     rest = get_group_name(arg, &group_name_end);
  4253.  
  4254.     if (rest != NULL)
  4255.     {
  4256.     syn_id = syn_check_group(arg, (int)(group_name_end - arg));
  4257.  
  4258.     /* allocate a buffer, for removing the backslashes in the keyword */
  4259.     keyword_copy = alloc((unsigned)STRLEN(rest) + 1);
  4260.     if (keyword_copy != NULL)
  4261.     {
  4262.         /*
  4263.          * The options given apply to ALL keywords, so all options must be
  4264.          * found before keywords can be created.
  4265.          * round 1: collect the options.
  4266.          * round 2: create the keywords.
  4267.          */
  4268.         first_arg = rest;
  4269.         for (round = 1; round <= 2; ++round)
  4270.         {
  4271.         /*
  4272.          * Isolate each keyword and add an entry for it.
  4273.          */
  4274.         for (rest = first_arg; rest != NULL && !ends_excmd(*rest);
  4275.                                rest = skipwhite(rest))
  4276.         {
  4277.             rest = get_syn_options(rest, &flags, TRUE, NULL,
  4278.                          NULL, &cont_in_list, &next_list);
  4279.             if (rest == NULL || ends_excmd(*rest))
  4280.             break;
  4281.             p = keyword_copy;
  4282.             while (*rest != 0 && !vim_iswhite(*rest))
  4283.             {
  4284.             if (*rest == '\\' && rest[1] != NUL)
  4285.                 ++rest;
  4286.             *p++ = *rest++;
  4287.             }
  4288.             *p = NUL;
  4289.             if (round == 2 && !eap->skip)
  4290.             {
  4291.             for (p = vim_strchr(keyword_copy, '['); ; )
  4292.             {
  4293.                 if (p != NULL)
  4294.                 *p = NUL;
  4295.                 add_keyword(keyword_copy, syn_id, flags,
  4296.                              cont_in_list, next_list);
  4297.                 if (p == NULL || p[1] == NUL || p[1] == ']')
  4298.                 break;
  4299. #ifdef FEAT_MBYTE
  4300.                 if (has_mbyte)
  4301.                 {
  4302.                 int l = (*mb_ptr2len_check)(p + 1);
  4303.  
  4304.                 mch_memmove(p, p + 1, l);
  4305.                 p += l;
  4306.                 }
  4307.                 else
  4308. #endif
  4309.                 {
  4310.                 p[0] = p[1];
  4311.                 ++p;
  4312.                 }
  4313.             }
  4314.             }
  4315.         }
  4316.         if (round == 1)
  4317.             syn_incl_toplevel(syn_id, &flags);
  4318.         }
  4319.         vim_free(keyword_copy);
  4320.     }
  4321.     }
  4322.  
  4323.     if (rest != NULL)
  4324.     eap->nextcmd = check_nextcmd(rest);
  4325.     else
  4326.     EMSG2(_(e_invarg2), arg);
  4327.  
  4328.     vim_free(cont_in_list);
  4329.     vim_free(next_list);
  4330.     redraw_curbuf_later(NOT_VALID);
  4331.     syn_stack_free_all(curbuf);        /* Need to recompute all syntax. */
  4332. }
  4333.  
  4334. /*
  4335.  * Handle ":syntax match {name} [{options}] {pattern} [{options}]".
  4336.  *
  4337.  * Also ":syntax sync match {name} [[grouphere | groupthere] {group-name}] .."
  4338.  */
  4339.     static void
  4340. syn_cmd_match(eap, syncing)
  4341.     exarg_T    *eap;
  4342.     int        syncing;        /* TRUE for ":syntax sync match .. " */
  4343. {
  4344.     char_u    *arg = eap->arg;
  4345.     char_u    *group_name_end;
  4346.     char_u    *rest;
  4347.     synpat_T    item;        /* the item found in the line */
  4348.     int        syn_id;
  4349.     int        idx;
  4350.     int        flags = 0;
  4351.     int        sync_idx = 0;
  4352.     short    *cont_list = NULL;
  4353.     short    *cont_in_list = NULL;
  4354.     short    *next_list = NULL;
  4355.  
  4356.     /* Isolate the group name, check for validity */
  4357.     rest = get_group_name(arg, &group_name_end);
  4358.  
  4359.     /* Get options before the pattern */
  4360.     rest = get_syn_options(rest, &flags, FALSE,
  4361.        syncing ? &sync_idx : NULL, &cont_list, &cont_in_list, &next_list);
  4362.  
  4363.     /* get the pattern. */
  4364.     init_syn_patterns();
  4365.     vim_memset(&item, 0, sizeof(item));
  4366.     rest = get_syn_pattern(rest, &item);
  4367.     if (vim_regcomp_had_eol() && !(flags & HL_EXCLUDENL))
  4368.     flags |= HL_HAS_EOL;
  4369.  
  4370.     /* Get options after the pattern */
  4371.     rest = get_syn_options(rest, &flags, FALSE,
  4372.        syncing ? &sync_idx : NULL, &cont_list, &cont_in_list, &next_list);
  4373.  
  4374.     if (rest != NULL)        /* all arguments are valid */
  4375.     {
  4376.     /*
  4377.      * Check for trailing command and illegal trailing arguments.
  4378.      */
  4379.     eap->nextcmd = check_nextcmd(rest);
  4380.     if (!ends_excmd(*rest) || eap->skip)
  4381.         rest = NULL;
  4382.     else if (ga_grow(&curbuf->b_syn_patterns, 1) != FAIL
  4383.         && (syn_id = syn_check_group(arg,
  4384.                        (int)(group_name_end - arg))) != 0)
  4385.     {
  4386.         syn_incl_toplevel(syn_id, &flags);
  4387.         /*
  4388.          * Store the pattern in the syn_items list
  4389.          */
  4390.         idx = curbuf->b_syn_patterns.ga_len;
  4391.         SYN_ITEMS(curbuf)[idx] = item;
  4392.         SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
  4393.         SYN_ITEMS(curbuf)[idx].sp_type = SPTYPE_MATCH;
  4394.         SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
  4395.         SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
  4396.         SYN_ITEMS(curbuf)[idx].sp_flags = flags;
  4397.         SYN_ITEMS(curbuf)[idx].sp_sync_idx = sync_idx;
  4398.         SYN_ITEMS(curbuf)[idx].sp_cont_list = cont_list;
  4399.         SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list = cont_in_list;
  4400.         SYN_ITEMS(curbuf)[idx].sp_next_list = next_list;
  4401.         ++curbuf->b_syn_patterns.ga_len;
  4402.         --curbuf->b_syn_patterns.ga_room;
  4403.  
  4404.         /* remember that we found a match for syncing on */
  4405.         if (flags & (HL_SYNC_HERE|HL_SYNC_THERE))
  4406.         curbuf->b_syn_sync_flags |= SF_MATCH;
  4407. #ifdef FEAT_FOLDING
  4408.         if (flags & HL_FOLD)
  4409.         ++curbuf->b_syn_folditems;
  4410. #endif
  4411.  
  4412.         redraw_curbuf_later(NOT_VALID);
  4413.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  4414.         return;    /* don't free the progs and patterns now */
  4415.     }
  4416.     }
  4417.  
  4418.     /*
  4419.      * Something failed, free the allocated memory.
  4420.      */
  4421.     vim_free(item.sp_prog);
  4422.     vim_free(item.sp_pattern);
  4423.     vim_free(cont_list);
  4424.     vim_free(cont_in_list);
  4425.     vim_free(next_list);
  4426.  
  4427.     if (rest == NULL)
  4428.     EMSG2(_(e_invarg2), arg);
  4429. }
  4430.  
  4431. /*
  4432.  * Handle ":syntax region {group-name} [matchgroup={group-name}]
  4433.  *        start {start} .. [skip {skip}] end {end} .. [{options}]".
  4434.  */
  4435.     static void
  4436. syn_cmd_region(eap, syncing)
  4437.     exarg_T    *eap;
  4438.     int        syncing;        /* TRUE for ":syntax sync region .." */
  4439. {
  4440.     char_u        *arg = eap->arg;
  4441.     char_u        *group_name_end;
  4442.     char_u        *rest;            /* next arg, NULL on error */
  4443.     char_u        *key_end;
  4444.     char_u        *key = NULL;
  4445.     char_u        *p;
  4446.     int            item;
  4447. #define ITEM_START        0
  4448. #define ITEM_SKIP        1
  4449. #define ITEM_END        2
  4450. #define ITEM_MATCHGROUP        3
  4451.     struct pat_ptr
  4452.     {
  4453.     synpat_T    *pp_synp;        /* pointer to syn_pattern */
  4454.     int        pp_matchgroup_id;    /* matchgroup ID */
  4455.     struct pat_ptr    *pp_next;        /* pointer to next pat_ptr */
  4456.     }            *(pat_ptrs[3]);
  4457.                     /* patterns found in the line */
  4458.     struct pat_ptr    *ppp;
  4459.     struct pat_ptr    *ppp_next;
  4460.     int            pat_count = 0;        /* nr of syn_patterns found */
  4461.     int            syn_id;
  4462.     int            matchgroup_id = 0;
  4463.     int            not_enough = FALSE;    /* not enough arguments */
  4464.     int            illegal = FALSE;    /* illegal arguments */
  4465.     int            success = FALSE;
  4466.     int            idx;
  4467.     int            flags = 0;
  4468.     short        *cont_list = NULL;
  4469.     short        *cont_in_list = NULL;
  4470.     short        *next_list = NULL;
  4471.  
  4472.     /* Isolate the group name, check for validity */
  4473.     rest = get_group_name(arg, &group_name_end);
  4474.  
  4475.     pat_ptrs[0] = NULL;
  4476.     pat_ptrs[1] = NULL;
  4477.     pat_ptrs[2] = NULL;
  4478.  
  4479.     init_syn_patterns();
  4480.  
  4481.     /*
  4482.      * get the options, patterns and matchgroup.
  4483.      */
  4484.     while (rest != NULL && !ends_excmd(*rest))
  4485.     {
  4486.     /* Check for option arguments */
  4487.     rest = get_syn_options(rest, &flags, FALSE, NULL,
  4488.                        &cont_list, &cont_in_list, &next_list);
  4489.     if (rest == NULL || ends_excmd(*rest))
  4490.         break;
  4491.  
  4492.     /* must be a pattern or matchgroup then */
  4493.     key_end = rest;
  4494.     while (*key_end && !vim_iswhite(*key_end) && *key_end != '=')
  4495.         ++key_end;
  4496.     vim_free(key);
  4497.     key = vim_strnsave_up(rest, (int)(key_end - rest));
  4498.     if (key == NULL)            /* out of memory */
  4499.     {
  4500.         rest = NULL;
  4501.         break;
  4502.     }
  4503.     if (STRCMP(key, "MATCHGROUP") == 0)
  4504.         item = ITEM_MATCHGROUP;
  4505.     else if (STRCMP(key, "START") == 0)
  4506.         item = ITEM_START;
  4507.     else if (STRCMP(key, "END") == 0)
  4508.         item = ITEM_END;
  4509.     else if (STRCMP(key, "SKIP") == 0)
  4510.     {
  4511.         if (pat_ptrs[ITEM_SKIP] != NULL)    /* one skip pattern allowed */
  4512.         {
  4513.         illegal = TRUE;
  4514.         break;
  4515.         }
  4516.         item = ITEM_SKIP;
  4517.     }
  4518.     else
  4519.         break;
  4520.     rest = skipwhite(key_end);
  4521.     if (*rest != '=')
  4522.     {
  4523.         rest = NULL;
  4524.         EMSG2(_("E398: Missing '=': %s"), arg);
  4525.         break;
  4526.     }
  4527.     rest = skipwhite(rest + 1);
  4528.     if (*rest == NUL)
  4529.     {
  4530.         not_enough = TRUE;
  4531.         break;
  4532.     }
  4533.  
  4534.     if (item == ITEM_MATCHGROUP)
  4535.     {
  4536.         p = skiptowhite(rest);
  4537.         if ((p - rest == 4 && STRNCMP(rest, "NONE", 4) == 0) || eap->skip)
  4538.         matchgroup_id = 0;
  4539.         else
  4540.         {
  4541.         matchgroup_id = syn_check_group(rest, (int)(p - rest));
  4542.         if (matchgroup_id == 0)
  4543.         {
  4544.             illegal = TRUE;
  4545.             break;
  4546.         }
  4547.         }
  4548.         rest = skipwhite(p);
  4549.     }
  4550.     else
  4551.     {
  4552.         /*
  4553.          * Allocate room for a syn_pattern, and link it in the list of
  4554.          * syn_patterns for this item, at the start (because the list is
  4555.          * used from end to start).
  4556.          */
  4557.         ppp = (struct pat_ptr *)alloc((unsigned)sizeof(struct pat_ptr));
  4558.         if (ppp == NULL)
  4559.         {
  4560.         rest = NULL;
  4561.         break;
  4562.         }
  4563.         ppp->pp_next = pat_ptrs[item];
  4564.         pat_ptrs[item] = ppp;
  4565.         ppp->pp_synp = (synpat_T *)alloc_clear((unsigned)sizeof(synpat_T));
  4566.         if (ppp->pp_synp == NULL)
  4567.         {
  4568.         rest = NULL;
  4569.         break;
  4570.         }
  4571.  
  4572.         /*
  4573.          * Get the syntax pattern and the following offset(s).
  4574.          */
  4575.         /* Enable the appropriate \z specials. */
  4576.         if (item == ITEM_START)
  4577.         reg_do_extmatch = REX_SET;
  4578.         else if (item == ITEM_SKIP || item == ITEM_END)
  4579.         reg_do_extmatch = REX_USE;
  4580.         rest = get_syn_pattern(rest, ppp->pp_synp);
  4581.         reg_do_extmatch = 0;
  4582.         if (item == ITEM_END && vim_regcomp_had_eol()
  4583.                            && !(flags & HL_EXCLUDENL))
  4584.         ppp->pp_synp->sp_flags |= HL_HAS_EOL;
  4585.         ppp->pp_matchgroup_id = matchgroup_id;
  4586.         ++pat_count;
  4587.     }
  4588.     }
  4589.     vim_free(key);
  4590.     if (illegal || not_enough)
  4591.     rest = NULL;
  4592.  
  4593.     /*
  4594.      * Must have a "start" and "end" pattern.
  4595.      */
  4596.     if (rest != NULL && (pat_ptrs[ITEM_START] == NULL ||
  4597.                           pat_ptrs[ITEM_END] == NULL))
  4598.     {
  4599.     not_enough = TRUE;
  4600.     rest = NULL;
  4601.     }
  4602.  
  4603.     if (rest != NULL)
  4604.     {
  4605.     /*
  4606.      * Check for trailing garbage or command.
  4607.      * If OK, add the item.
  4608.      */
  4609.     eap->nextcmd = check_nextcmd(rest);
  4610.     if (!ends_excmd(*rest) || eap->skip)
  4611.         rest = NULL;
  4612.     else if (ga_grow(&(curbuf->b_syn_patterns), pat_count) != FAIL
  4613.         && (syn_id = syn_check_group(arg,
  4614.                        (int)(group_name_end - arg))) != 0)
  4615.     {
  4616.         syn_incl_toplevel(syn_id, &flags);
  4617.         /*
  4618.          * Store the start/skip/end in the syn_items list
  4619.          */
  4620.         idx = curbuf->b_syn_patterns.ga_len;
  4621.         for (item = ITEM_START; item <= ITEM_END; ++item)
  4622.         {
  4623.         for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp->pp_next)
  4624.         {
  4625.             SYN_ITEMS(curbuf)[idx] = *(ppp->pp_synp);
  4626.             SYN_ITEMS(curbuf)[idx].sp_syncing = syncing;
  4627.             SYN_ITEMS(curbuf)[idx].sp_type =
  4628.                 (item == ITEM_START) ? SPTYPE_START :
  4629.                 (item == ITEM_SKIP) ? SPTYPE_SKIP : SPTYPE_END;
  4630.             SYN_ITEMS(curbuf)[idx].sp_flags |= flags;
  4631.             SYN_ITEMS(curbuf)[idx].sp_syn.id = syn_id;
  4632.             SYN_ITEMS(curbuf)[idx].sp_syn.inc_tag = current_syn_inc_tag;
  4633.             SYN_ITEMS(curbuf)[idx].sp_syn_match_id =
  4634.                             ppp->pp_matchgroup_id;
  4635.             if (item == ITEM_START)
  4636.             {
  4637.             SYN_ITEMS(curbuf)[idx].sp_cont_list = cont_list;
  4638.             SYN_ITEMS(curbuf)[idx].sp_syn.cont_in_list =
  4639.                                  cont_in_list;
  4640.             SYN_ITEMS(curbuf)[idx].sp_next_list = next_list;
  4641.             }
  4642.             ++curbuf->b_syn_patterns.ga_len;
  4643.             --curbuf->b_syn_patterns.ga_room;
  4644.             ++idx;
  4645. #ifdef FEAT_FOLDING
  4646.             if (flags & HL_FOLD)
  4647.             ++curbuf->b_syn_folditems;
  4648. #endif
  4649.         }
  4650.         }
  4651.  
  4652.         redraw_curbuf_later(NOT_VALID);
  4653.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  4654.         success = TRUE;        /* don't free the progs and patterns now */
  4655.     }
  4656.     }
  4657.  
  4658.     /*
  4659.      * Free the allocated memory.
  4660.      */
  4661.     for (item = ITEM_START; item <= ITEM_END; ++item)
  4662.     for (ppp = pat_ptrs[item]; ppp != NULL; ppp = ppp_next)
  4663.     {
  4664.         if (!success)
  4665.         {
  4666.         vim_free(ppp->pp_synp->sp_prog);
  4667.         vim_free(ppp->pp_synp->sp_pattern);
  4668.         }
  4669.         vim_free(ppp->pp_synp);
  4670.         ppp_next = ppp->pp_next;
  4671.         vim_free(ppp);
  4672.     }
  4673.  
  4674.     if (!success)
  4675.     {
  4676.     vim_free(cont_list);
  4677.     vim_free(cont_in_list);
  4678.     vim_free(next_list);
  4679.     if (not_enough)
  4680.         EMSG2(_("E399: Not enough arguments: syntax region %s"), arg);
  4681.     else if (illegal || rest == NULL)
  4682.         EMSG2(_(e_invarg2), arg);
  4683.     }
  4684. }
  4685.  
  4686. /*
  4687.  * A simple syntax group ID comparison function suitable for use in qsort()
  4688.  */
  4689.     static int
  4690. #ifdef __BORLANDC__
  4691. _RTLENTRYF
  4692. #endif
  4693. syn_compare_stub(v1, v2)
  4694.     const void    *v1;
  4695.     const void    *v2;
  4696. {
  4697.     const short    *s1 = v1;
  4698.     const short    *s2 = v2;
  4699.  
  4700.     return (*s1 > *s2 ? 1 : *s1 < *s2 ? -1 : 0);
  4701. }
  4702.  
  4703. /*
  4704.  * Combines lists of syntax clusters.
  4705.  * *clstr1 and *clstr2 must both be allocated memory; they will be consumed.
  4706.  */
  4707.     static void
  4708. syn_combine_list(clstr1, clstr2, list_op)
  4709.     short    **clstr1;
  4710.     short    **clstr2;
  4711.     int        list_op;
  4712. {
  4713.     int        count1 = 0;
  4714.     int        count2 = 0;
  4715.     short    *g1;
  4716.     short    *g2;
  4717.     short    *clstr = NULL;
  4718.     int        count;
  4719.     int        round;
  4720.  
  4721.     /*
  4722.      * Handle degenerate cases.
  4723.      */
  4724.     if (*clstr2 == NULL)
  4725.     return;
  4726.     if (*clstr1 == NULL || list_op == CLUSTER_REPLACE)
  4727.     {
  4728.     if (list_op == CLUSTER_REPLACE)
  4729.         vim_free(*clstr1);
  4730.     if (list_op == CLUSTER_REPLACE || list_op == CLUSTER_ADD)
  4731.         *clstr1 = *clstr2;
  4732.     else
  4733.         vim_free(*clstr2);
  4734.     return;
  4735.     }
  4736.  
  4737.     for (g1 = *clstr1; *g1; g1++)
  4738.     ++count1;
  4739.     for (g2 = *clstr2; *g2; g2++)
  4740.     ++count2;
  4741.  
  4742.     /*
  4743.      * For speed purposes, sort both lists.
  4744.      */
  4745.     qsort(*clstr1, (size_t)count1, sizeof(short), syn_compare_stub);
  4746.     qsort(*clstr2, (size_t)count2, sizeof(short), syn_compare_stub);
  4747.  
  4748.     /*
  4749.      * We proceed in two passes; in round 1, we count the elements to place
  4750.      * in the new list, and in round 2, we allocate and populate the new
  4751.      * list.  For speed, we use a mergesort-like method, adding the smaller
  4752.      * of the current elements in each list to the new list.
  4753.      */
  4754.     for (round = 1; round <= 2; round++)
  4755.     {
  4756.     g1 = *clstr1;
  4757.     g2 = *clstr2;
  4758.     count = 0;
  4759.  
  4760.     /*
  4761.      * First, loop through the lists until one of them is empty.
  4762.      */
  4763.     while (*g1 && *g2)
  4764.     {
  4765.         /*
  4766.          * We always want to add from the first list.
  4767.          */
  4768.         if (*g1 < *g2)
  4769.         {
  4770.         if (round == 2)
  4771.             clstr[count] = *g1;
  4772.         count++;
  4773.         g1++;
  4774.         continue;
  4775.         }
  4776.         /*
  4777.          * We only want to add from the second list if we're adding the
  4778.          * lists.
  4779.          */
  4780.         if (list_op == CLUSTER_ADD)
  4781.         {
  4782.         if (round == 2)
  4783.             clstr[count] = *g2;
  4784.         count++;
  4785.         }
  4786.         if (*g1 == *g2)
  4787.         g1++;
  4788.         g2++;
  4789.     }
  4790.  
  4791.     /*
  4792.      * Now add the leftovers from whichever list didn't get finished
  4793.      * first.  As before, we only want to add from the second list if
  4794.      * we're adding the lists.
  4795.      */
  4796.     for (; *g1; g1++, count++)
  4797.         if (round == 2)
  4798.         clstr[count] = *g1;
  4799.     if (list_op == CLUSTER_ADD)
  4800.         for (; *g2; g2++, count++)
  4801.         if (round == 2)
  4802.             clstr[count] = *g2;
  4803.  
  4804.     if (round == 1)
  4805.     {
  4806.         /*
  4807.          * If the group ended up empty, we don't need to allocate any
  4808.          * space for it.
  4809.          */
  4810.         if (count == 0)
  4811.         {
  4812.         clstr = NULL;
  4813.         break;
  4814.         }
  4815.         clstr = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
  4816.         if (clstr == NULL)
  4817.         break;
  4818.         clstr[count] = 0;
  4819.     }
  4820.     }
  4821.  
  4822.     /*
  4823.      * Finally, put the new list in place.
  4824.      */
  4825.     vim_free(*clstr1);
  4826.     vim_free(*clstr2);
  4827.     *clstr1 = clstr;
  4828. }
  4829.  
  4830. /*
  4831.  * Lookup a syntax cluster name and return it's ID.
  4832.  * If it is not found, 0 is returned.
  4833.  */
  4834.     static int
  4835. syn_scl_name2id(name)
  4836.     char_u    *name;
  4837. {
  4838.     int        i;
  4839.     char_u    *name_u;
  4840.  
  4841.     /* Avoid using stricmp() too much, it's slow on some systems */
  4842.     name_u = vim_strsave_up(name);
  4843.     if (name_u == NULL)
  4844.     return 0;
  4845.     for (i = curbuf->b_syn_clusters.ga_len; --i >= 0; )
  4846.     if (SYN_CLSTR(curbuf)[i].scl_name_u != NULL
  4847.         && STRCMP(name_u, SYN_CLSTR(curbuf)[i].scl_name_u) == 0)
  4848.         break;
  4849.     vim_free(name_u);
  4850.     return (i < 0 ? 0 : i + SYNID_CLUSTER);
  4851. }
  4852.  
  4853. /*
  4854.  * Like syn_scl_name2id(), but take a pointer + length argument.
  4855.  */
  4856.     static int
  4857. syn_scl_namen2id(linep, len)
  4858.     char_u  *linep;
  4859.     int        len;
  4860. {
  4861.     char_u  *name;
  4862.     int        id = 0;
  4863.  
  4864.     name = vim_strnsave(linep, len);
  4865.     if (name != NULL)
  4866.     {
  4867.     id = syn_scl_name2id(name);
  4868.     vim_free(name);
  4869.     }
  4870.     return id;
  4871. }
  4872.  
  4873. /*
  4874.  * Find syntax cluster name in the table and return it's ID.
  4875.  * The argument is a pointer to the name and the length of the name.
  4876.  * If it doesn't exist yet, a new entry is created.
  4877.  * Return 0 for failure.
  4878.  */
  4879.     static int
  4880. syn_check_cluster(pp, len)
  4881.     char_u    *pp;
  4882.     int        len;
  4883. {
  4884.     int        id;
  4885.     char_u    *name;
  4886.  
  4887.     name = vim_strnsave(pp, len);
  4888.     if (name == NULL)
  4889.     return 0;
  4890.  
  4891.     id = syn_scl_name2id(name);
  4892.     if (id == 0)            /* doesn't exist yet */
  4893.     id = syn_add_cluster(name);
  4894.     else
  4895.     vim_free(name);
  4896.     return id;
  4897. }
  4898.  
  4899. /*
  4900.  * Add new syntax cluster and return it's ID.
  4901.  * "name" must be an allocated string, it will be consumed.
  4902.  * Return 0 for failure.
  4903.  */
  4904.     static int
  4905. syn_add_cluster(name)
  4906.     char_u        *name;
  4907. {
  4908.     int len;
  4909.  
  4910.     /*
  4911.      * First call for this growarray: init growing array.
  4912.      */
  4913.     if (curbuf->b_syn_clusters.ga_data == NULL)
  4914.     {
  4915.     curbuf->b_syn_clusters.ga_itemsize = sizeof(struct syn_cluster);
  4916.     curbuf->b_syn_clusters.ga_growsize = 10;
  4917.     }
  4918.  
  4919.     /*
  4920.      * Make room for at least one other cluster entry.
  4921.      */
  4922.     if (ga_grow(&curbuf->b_syn_clusters, 1) == FAIL)
  4923.     {
  4924.     vim_free(name);
  4925.     return 0;
  4926.     }
  4927.     len = curbuf->b_syn_clusters.ga_len;
  4928.  
  4929.     vim_memset(&(SYN_CLSTR(curbuf)[len]), 0, sizeof(struct syn_cluster));
  4930.     SYN_CLSTR(curbuf)[len].scl_name = name;
  4931.     SYN_CLSTR(curbuf)[len].scl_name_u = vim_strsave_up(name);
  4932.     SYN_CLSTR(curbuf)[len].scl_list = NULL;
  4933.     ++curbuf->b_syn_clusters.ga_len;
  4934.     --curbuf->b_syn_clusters.ga_room;
  4935.  
  4936.     return len + SYNID_CLUSTER;
  4937. }
  4938.  
  4939. /*
  4940.  * Handle ":syntax cluster {cluster-name} [contains={groupname},..]
  4941.  *        [add={groupname},..] [remove={groupname},..]".
  4942.  */
  4943. /* ARGSUSED */
  4944.     static void
  4945. syn_cmd_cluster(eap, syncing)
  4946.     exarg_T    *eap;
  4947.     int        syncing;        /* not used */
  4948. {
  4949.     char_u    *arg = eap->arg;
  4950.     char_u    *group_name_end;
  4951.     char_u    *rest;
  4952.     int        scl_id;
  4953.     short    *clstr_list;
  4954.     int        got_clstr = FALSE;
  4955.     int        opt_len;
  4956.     int        list_op;
  4957.  
  4958.     eap->nextcmd = find_nextcmd(arg);
  4959.     if (eap->skip)
  4960.     return;
  4961.  
  4962.     rest = get_group_name(arg, &group_name_end);
  4963.  
  4964.     if (rest != NULL)
  4965.     {
  4966.     scl_id = syn_check_cluster(arg, (int)(group_name_end - arg))
  4967.             - SYNID_CLUSTER;
  4968.  
  4969.     for (;;)
  4970.     {
  4971.         if (STRNICMP(rest, "add", 3) == 0
  4972.             && (vim_iswhite(rest[3]) || rest[3] == '='))
  4973.         {
  4974.         opt_len = 3;
  4975.         list_op = CLUSTER_ADD;
  4976.         }
  4977.         else if (STRNICMP(rest, "remove", 6) == 0
  4978.             && (vim_iswhite(rest[6]) || rest[6] == '='))
  4979.         {
  4980.         opt_len = 6;
  4981.         list_op = CLUSTER_SUBTRACT;
  4982.         }
  4983.         else if (STRNICMP(rest, "contains", 8) == 0
  4984.             && (vim_iswhite(rest[8]) || rest[8] == '='))
  4985.         {
  4986.         opt_len = 8;
  4987.         list_op = CLUSTER_REPLACE;
  4988.         }
  4989.         else
  4990.         break;
  4991.  
  4992.         clstr_list = NULL;
  4993.         if (get_id_list(&rest, opt_len, &clstr_list) == FAIL)
  4994.         {
  4995.         EMSG2(_(e_invarg2), rest);
  4996.         break;
  4997.         }
  4998.         syn_combine_list(&SYN_CLSTR(curbuf)[scl_id].scl_list,
  4999.                  &clstr_list, list_op);
  5000.         got_clstr = TRUE;
  5001.     }
  5002.  
  5003.     if (got_clstr)
  5004.     {
  5005.         redraw_curbuf_later(NOT_VALID);
  5006.         syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  5007.     }
  5008.     }
  5009.  
  5010.     if (!got_clstr)
  5011.     EMSG(_("E400: No cluster specified"));
  5012.     if (rest == NULL || !ends_excmd(*rest))
  5013.     EMSG2(_(e_invarg2), arg);
  5014. }
  5015.  
  5016. /*
  5017.  * On first call for current buffer: Init growing array.
  5018.  */
  5019.     static void
  5020. init_syn_patterns()
  5021. {
  5022.     curbuf->b_syn_patterns.ga_itemsize = sizeof(synpat_T);
  5023.     curbuf->b_syn_patterns.ga_growsize = 10;
  5024. }
  5025.  
  5026. /*
  5027.  * Get one pattern for a ":syntax match" or ":syntax region" command.
  5028.  * Stores the pattern and program in a synpat_T.
  5029.  * Returns a pointer to the next argument, or NULL in case of an error.
  5030.  */
  5031.     static char_u *
  5032. get_syn_pattern(arg, ci)
  5033.     char_u    *arg;
  5034.     synpat_T    *ci;
  5035. {
  5036.     char_u    *end;
  5037.     int        *p;
  5038.     int        idx;
  5039.     char_u    *cpo_save;
  5040.  
  5041.     /* need at least three chars */
  5042.     if (arg == NULL || arg[1] == NUL || arg[2] == NUL)
  5043.     return NULL;
  5044.  
  5045.     end = skip_regexp(arg + 1, *arg, TRUE);
  5046.     if (*end != *arg)                /* end delimiter not found */
  5047.     {
  5048.     EMSG2(_("E401: Pattern delimiter not found: %s"), arg);
  5049.     return NULL;
  5050.     }
  5051.     /* store the pattern and compiled regexp program */
  5052.     if ((ci->sp_pattern = vim_strnsave(arg + 1, (int)(end - arg - 1))) == NULL)
  5053.     return NULL;
  5054.  
  5055.     /* Make 'cpoptions' empty, to avoid the 'l' flag */
  5056.     cpo_save = p_cpo;
  5057.     p_cpo = (char_u *)"";
  5058.     ci->sp_prog = vim_regcomp(ci->sp_pattern, TRUE);
  5059.     p_cpo = cpo_save;
  5060.  
  5061.     if (ci->sp_prog == NULL)
  5062.     return NULL;
  5063.     ci->sp_ic = curbuf->b_syn_ic;
  5064.  
  5065.     /*
  5066.      * Check for a match, highlight or region offset.
  5067.      */
  5068.     ++end;
  5069.     do
  5070.     {
  5071.     for (idx = SPO_COUNT; --idx >= 0; )
  5072.         if (STRNCMP(end, spo_name_tab[idx], 3) == 0)
  5073.         break;
  5074.     if (idx >= 0)
  5075.     {
  5076.         p = &(ci->sp_offsets[idx]);
  5077.         if (idx != SPO_LC_OFF)
  5078.         switch (end[3])
  5079.         {
  5080.             case 's':   break;
  5081.             case 'b':   break;
  5082.             case 'e':   idx += SPO_COUNT; break;
  5083.             default:    idx = -1; break;
  5084.         }
  5085.         if (idx >= 0)
  5086.         {
  5087.         ci->sp_off_flags |= (1 << idx);
  5088.         if (idx == SPO_LC_OFF)        /* lc=99 */
  5089.         {
  5090.             end += 3;
  5091.             *p = getdigits(&end);
  5092.  
  5093.             /* "lc=" offset automatically sets "ms=" offset */
  5094.             if (!(ci->sp_off_flags & (1 << SPO_MS_OFF)))
  5095.             {
  5096.             ci->sp_off_flags |= (1 << SPO_MS_OFF);
  5097.             ci->sp_offsets[SPO_MS_OFF] = *p;
  5098.             }
  5099.         }
  5100.         else                /* yy=x+99 */
  5101.         {
  5102.             end += 4;
  5103.             if (*end == '+')
  5104.             {
  5105.             ++end;
  5106.             *p = getdigits(&end);        /* positive offset */
  5107.             }
  5108.             else if (*end == '-')
  5109.             {
  5110.             ++end;
  5111.             *p = -getdigits(&end);        /* negative offset */
  5112.             }
  5113.         }
  5114.         if (*end != ',')
  5115.             break;
  5116.         ++end;
  5117.         }
  5118.     }
  5119.     } while (idx >= 0);
  5120.  
  5121.     if (!ends_excmd(*end) && !vim_iswhite(*end))
  5122.     {
  5123.     EMSG2(_("E402: Garbage after pattern: %s"), arg);
  5124.     return NULL;
  5125.     }
  5126.     return skipwhite(end);
  5127. }
  5128.  
  5129. /*
  5130.  * Handle ":syntax sync .." command.
  5131.  */
  5132. /* ARGSUSED */
  5133.     static void
  5134. syn_cmd_sync(eap, syncing)
  5135.     exarg_T    *eap;
  5136.     int        syncing;        /* not used */
  5137. {
  5138.     char_u    *arg_start = eap->arg;
  5139.     char_u    *arg_end;
  5140.     char_u    *key = NULL;
  5141.     char_u    *next_arg;
  5142.     int        illegal = FALSE;
  5143.     int        finished = FALSE;
  5144.     long    n;
  5145.     char_u    *cpo_save;
  5146.  
  5147.     if (ends_excmd(*arg_start))
  5148.     {
  5149.     syn_cmd_list(eap, TRUE);
  5150.     return;
  5151.     }
  5152.  
  5153.     while (!ends_excmd(*arg_start))
  5154.     {
  5155.     arg_end = skiptowhite(arg_start);
  5156.     next_arg = skipwhite(arg_end);
  5157.     vim_free(key);
  5158.     key = vim_strnsave_up(arg_start, (int)(arg_end - arg_start));
  5159.     if (STRCMP(key, "CCOMMENT") == 0)
  5160.     {
  5161.         if (!eap->skip)
  5162.         curbuf->b_syn_sync_flags |= SF_CCOMMENT;
  5163.         if (!ends_excmd(*next_arg))
  5164.         {
  5165.         arg_end = skiptowhite(next_arg);
  5166.         if (!eap->skip)
  5167.             curbuf->b_syn_sync_id = syn_check_group(next_arg,
  5168.                            (int)(arg_end - next_arg));
  5169.         next_arg = skipwhite(arg_end);
  5170.         }
  5171.         else if (!eap->skip)
  5172.         curbuf->b_syn_sync_id = syn_name2id((char_u *)"Comment");
  5173.     }
  5174.     else if (  STRNCMP(key, "LINES", 5) == 0
  5175.         || STRNCMP(key, "MINLINES", 8) == 0
  5176.         || STRNCMP(key, "MAXLINES", 8) == 0)
  5177.     {
  5178.         if (key[0] == 'L')
  5179.         arg_end = key + 6;
  5180.         else
  5181.         arg_end = key + 9;
  5182.         if (arg_end[-1] != '=' || !isdigit(*arg_end))
  5183.         {
  5184.         illegal = TRUE;
  5185.         break;
  5186.         }
  5187.         n = getdigits(&arg_end);
  5188.         if (!eap->skip)
  5189.         {
  5190.         if (key[1] == 'A')
  5191.             curbuf->b_syn_sync_maxlines = n;
  5192.         else
  5193.             curbuf->b_syn_sync_minlines = n;
  5194.         }
  5195.     }
  5196.     else if (STRCMP(key, "FROMSTART") == 0)
  5197.     {
  5198.         curbuf->b_syn_sync_minlines = MAXLNUM;
  5199.         curbuf->b_syn_sync_maxlines = 0;
  5200.     }
  5201.     else if (STRCMP(key, "LINECONT") == 0)
  5202.     {
  5203.         if (curbuf->b_syn_linecont_pat != NULL)
  5204.         {
  5205.         EMSG(_("E403: syntax sync: line continuations pattern specified twice"));
  5206.         finished = TRUE;
  5207.         break;
  5208.         }
  5209.         arg_end = skip_regexp(next_arg + 1, *next_arg, TRUE);
  5210.         if (*arg_end != *next_arg)        /* end delimiter not found */
  5211.         {
  5212.         illegal = TRUE;
  5213.         break;
  5214.         }
  5215.  
  5216.         if (!eap->skip)
  5217.         {
  5218.         /* store the pattern and compiled regexp program */
  5219.         if ((curbuf->b_syn_linecont_pat = vim_strnsave(next_arg + 1,
  5220.                       (int)(arg_end - next_arg - 1))) == NULL)
  5221.         {
  5222.             finished = TRUE;
  5223.             break;
  5224.         }
  5225.         curbuf->b_syn_linecont_ic = curbuf->b_syn_ic;
  5226.  
  5227.         /* Make 'cpoptions' empty, to avoid the 'l' flag */
  5228.         cpo_save = p_cpo;
  5229.         p_cpo = (char_u *)"";
  5230.         curbuf->b_syn_linecont_prog =
  5231.                 vim_regcomp(curbuf->b_syn_linecont_pat, TRUE);
  5232.         p_cpo = cpo_save;
  5233.  
  5234.         if (curbuf->b_syn_linecont_prog == NULL)
  5235.         {
  5236.             vim_free(curbuf->b_syn_linecont_pat);
  5237.             curbuf->b_syn_linecont_pat = NULL;
  5238.             finished = TRUE;
  5239.             break;
  5240.         }
  5241.         }
  5242.         next_arg = skipwhite(arg_end + 1);
  5243.     }
  5244.     else
  5245.     {
  5246.         eap->arg = next_arg;
  5247.         if (STRCMP(key, "MATCH") == 0)
  5248.         syn_cmd_match(eap, TRUE);
  5249.         else if (STRCMP(key, "REGION") == 0)
  5250.         syn_cmd_region(eap, TRUE);
  5251.         else if (STRCMP(key, "CLEAR") == 0)
  5252.         syn_cmd_clear(eap, TRUE);
  5253.         else
  5254.         illegal = TRUE;
  5255.         finished = TRUE;
  5256.         break;
  5257.     }
  5258.     arg_start = next_arg;
  5259.     }
  5260.     vim_free(key);
  5261.     if (illegal)
  5262.     EMSG2(_("E404: Illegal arguments: %s"), arg_start);
  5263.     else if (!finished)
  5264.     {
  5265.     eap->nextcmd = check_nextcmd(arg_start);
  5266.     redraw_curbuf_later(NOT_VALID);
  5267.     syn_stack_free_all(curbuf);    /* Need to recompute all syntax. */
  5268.     }
  5269. }
  5270.  
  5271. /*
  5272.  * Convert a line of highlight group names into a list of group ID numbers.
  5273.  * "arg" should point to the "contains" or "nextgroup" keyword.
  5274.  * "arg" is advanced to after the last group name.
  5275.  * Careful: the argument is modified (NULs added).
  5276.  * returns FAIL for some error, OK for success.
  5277.  */
  5278.     static int
  5279. get_id_list(arg, keylen, list)
  5280.     char_u    **arg;
  5281.     int        keylen;        /* length of keyword */
  5282.     short    **list;        /* where to store the resulting list, if not
  5283.                    NULL, the list is silently skipped! */
  5284. {
  5285.     char_u    *p = NULL;
  5286.     char_u    *end;
  5287.     int        round;
  5288.     int        count;
  5289.     int        total_count = 0;
  5290.     short    *retval = NULL;
  5291.     char_u    *name;
  5292.     regmatch_T    regmatch;
  5293.     int        id;
  5294.     int        i;
  5295.     int        failed = FALSE;
  5296.  
  5297.     /*
  5298.      * We parse the list twice:
  5299.      * round == 1: count the number of items, allocate the array.
  5300.      * round == 2: fill the array with the items.
  5301.      * In round 1 new groups may be added, causing the number of items to
  5302.      * grow when a regexp is used.  In that case round 1 is done once again.
  5303.      */
  5304.     for (round = 1; round <= 2; ++round)
  5305.     {
  5306.     /*
  5307.      * skip "contains"
  5308.      */
  5309.     p = skipwhite(*arg + keylen);
  5310.     if (*p != '=')
  5311.     {
  5312.         EMSG2(_("E405: Missing equal sign: %s"), *arg);
  5313.         break;
  5314.     }
  5315.     p = skipwhite(p + 1);
  5316.     if (ends_excmd(*p))
  5317.     {
  5318.         EMSG2(_("E406: Empty argument: %s"), *arg);
  5319.         break;
  5320.     }
  5321.  
  5322.     /*
  5323.      * parse the arguments after "contains"
  5324.      */
  5325.     count = 0;
  5326.     while (!ends_excmd(*p))
  5327.     {
  5328.         for (end = p; *end && !vim_iswhite(*end) && *end != ','; ++end)
  5329.         ;
  5330.         name = alloc((int)(end - p + 3));        /* leave room for "^$" */
  5331.         if (name == NULL)
  5332.         {
  5333.         failed = TRUE;
  5334.         break;
  5335.         }
  5336.         STRNCPY(name + 1, p, end - p);
  5337.         name[end - p + 1] = NUL;
  5338.         if (       STRCMP(name + 1, "ALLBUT") == 0
  5339.             || STRCMP(name + 1, "ALL") == 0
  5340.             || STRCMP(name + 1, "TOP") == 0
  5341.             || STRCMP(name + 1, "CONTAINED") == 0)
  5342.         {
  5343.         if (TO_UPPER(**arg) != 'C')
  5344.         {
  5345.             EMSG2(_("E407: %s not allowed here"), name + 1);
  5346.             failed = TRUE;
  5347.             vim_free(name);
  5348.             break;
  5349.         }
  5350.         if (count != 0)
  5351.         {
  5352.             EMSG2(_("E408: %s must be first in contains list"), name + 1);
  5353.             failed = TRUE;
  5354.             vim_free(name);
  5355.             break;
  5356.         }
  5357.         if (name[1] == 'A')
  5358.             id = SYNID_ALLBUT;
  5359.         else if (name[1] == 'T')
  5360.             id = SYNID_TOP;
  5361.         else
  5362.             id = SYNID_CONTAINED;
  5363.         id += current_syn_inc_tag;
  5364.         }
  5365.         else if (name[1] == '@')
  5366.         {
  5367.         id = syn_check_cluster(name + 2, (int)(end - p - 1));
  5368.         }
  5369.         else
  5370.         {
  5371.         /*
  5372.          * Handle full group name.
  5373.          */
  5374.         if (vim_strpbrk(name + 1, (char_u *)"\\.*^$~[") == NULL)
  5375.             id = syn_check_group(name + 1, (int)(end - p));
  5376.         else
  5377.         {
  5378.             /*
  5379.              * Handle match of regexp with group names.
  5380.              */
  5381.             *name = '^';
  5382.             STRCAT(name, "$");
  5383.             regmatch.regprog = vim_regcomp(name, TRUE);
  5384.             if (regmatch.regprog == NULL)
  5385.             {
  5386.             failed = TRUE;
  5387.             vim_free(name);
  5388.             break;
  5389.             }
  5390.  
  5391.             regmatch.rm_ic = TRUE;
  5392.             id = 0;
  5393.             for (i = highlight_ga.ga_len; --i >= 0; )
  5394.             {
  5395.             if (vim_regexec(®match, HL_TABLE()[i].sg_name,
  5396.                                   (colnr_T)0))
  5397.             {
  5398.                 if (round == 2)
  5399.                 {
  5400.                 /* Got more items than expected; can happen
  5401.                  * when adding items that match:
  5402.                  * "contains=a.*b,axb".
  5403.                  * Go back to first round */
  5404.                 if (count >= total_count)
  5405.                 {
  5406.                     vim_free(retval);
  5407.                     round = 1;
  5408.                 }
  5409.                 else
  5410.                     retval[count] = i + 1;
  5411.                 }
  5412.                 ++count;
  5413.                 id = -1;        /* remember that we found one */
  5414.             }
  5415.             }
  5416.             vim_free(regmatch.regprog);
  5417.         }
  5418.         }
  5419.         vim_free(name);
  5420.         if (id == 0)
  5421.         {
  5422.         EMSG2(_("E409: Unknown group name: %s"), p);
  5423.         failed = TRUE;
  5424.         break;
  5425.         }
  5426.         if (id > 0)
  5427.         {
  5428.         if (round == 2)
  5429.         {
  5430.             /* Got more items than expected, go back to first round */
  5431.             if (count >= total_count)
  5432.             {
  5433.             vim_free(retval);
  5434.             round = 1;
  5435.             }
  5436.             else
  5437.             retval[count] = id;
  5438.         }
  5439.         ++count;
  5440.         }
  5441.         p = skipwhite(end);
  5442.         if (*p != ',')
  5443.         break;
  5444.         p = skipwhite(p + 1);    /* skip comma in between arguments */
  5445.     }
  5446.     if (failed)
  5447.         break;
  5448.     if (round == 1)
  5449.     {
  5450.         retval = (short *)alloc((unsigned)((count + 1) * sizeof(short)));
  5451.         if (retval == NULL)
  5452.         break;
  5453.         retval[count] = 0;        /* zero means end of the list */
  5454.         total_count = count;
  5455.     }
  5456.     }
  5457.  
  5458.     *arg = p;
  5459.     if (failed || retval == NULL)
  5460.     {
  5461.     vim_free(retval);
  5462.     return FAIL;
  5463.     }
  5464.  
  5465.     if (*list == NULL)
  5466.     *list = retval;
  5467.     else
  5468.     vim_free(retval);    /* list already found, don't overwrite it */
  5469.  
  5470.     return OK;
  5471. }
  5472.  
  5473. /*
  5474.  * Make a copy of an ID list.
  5475.  */
  5476.     static short *
  5477. copy_id_list(list)
  5478.     short   *list;
  5479. {
  5480.     int        len;
  5481.     int        count;
  5482.     short   *retval;
  5483.  
  5484.     if (list == NULL)
  5485.     return NULL;
  5486.  
  5487.     for (count = 0; list[count]; ++count)
  5488.     ;
  5489.     len = (count + 1) * sizeof(short);
  5490.     retval = (short *)alloc((unsigned)len);
  5491.     if (retval != NULL)
  5492.     mch_memmove(retval, list, (size_t)len);
  5493.  
  5494.     return retval;
  5495. }
  5496.  
  5497. /*
  5498.  * Check if syntax group "ssp" is in the ID list "list" of "cur_si".
  5499.  * "cur_si" can be NULL if not checking the "containedin" list.
  5500.  * Used to check if a syntax item is in the "contains" or "nextgroup" list of
  5501.  * the current item.
  5502.  * This function is called very often, keep it fast!!
  5503.  */
  5504.     static int
  5505. in_id_list(cur_si, list, ssp, contained)
  5506.     stateitem_T    *cur_si;    /* current item or NULL */
  5507.     short    *list;        /* id list */
  5508.     struct sp_syn *ssp;        /* group id and ":syn include" tag of group */
  5509.     int        contained;    /* group id is contained */
  5510. {
  5511.     int        retval;
  5512.     short    *scl_list;
  5513.     short    item;
  5514.     short    id = ssp->id;
  5515.     static int    depth = 0;
  5516.     int        r;
  5517.  
  5518.     /* If spp has a "containedin" list and "cur_si" is in it, return TRUE. */
  5519.     if (cur_si != NULL
  5520.         && ssp->cont_in_list != NULL
  5521.         && in_id_list(NULL, ssp->cont_in_list,
  5522.         &(SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_syn),
  5523.           SYN_ITEMS(syn_buf)[cur_si->si_idx].sp_flags & HL_CONTAINED))
  5524.     return TRUE;
  5525.  
  5526.     /*
  5527.      * If list is ID_LIST_ALL, we are in a transparent item that isn't
  5528.      * inside anything.  Only allow not-contained groups.
  5529.      */
  5530.     if (list == ID_LIST_ALL)
  5531.     return !contained;
  5532.  
  5533.     /*
  5534.      * If the first item is "ALLBUT", return TRUE if "id" is NOT in the
  5535.      * contains list.  We also require that "id" is at the same ":syn include"
  5536.      * level as the list.
  5537.      */
  5538.     item = *list;
  5539.     if (item >= SYNID_ALLBUT && item < SYNID_CLUSTER)
  5540.     {
  5541.     if (item < SYNID_TOP)
  5542.     {
  5543.         /* ALL or ALLBUT: accept all groups in the same file */
  5544.         if (item - SYNID_ALLBUT != ssp->inc_tag)
  5545.         return FALSE;
  5546.     }
  5547.     else if (item < SYNID_CONTAINED)
  5548.     {
  5549.         /* TOP: accept all not-contained groups in the same file */
  5550.         if (item - SYNID_TOP != ssp->inc_tag || contained)
  5551.         return FALSE;
  5552.     }
  5553.     else
  5554.     {
  5555.         /* CONTAINED: accept all contained groups in the same file */
  5556.         if (item - SYNID_CONTAINED != ssp->inc_tag || !contained)
  5557.         return FALSE;
  5558.     }
  5559.     item = *++list;
  5560.     retval = FALSE;
  5561.     }
  5562.     else
  5563.     retval = TRUE;
  5564.  
  5565.     /*
  5566.      * Return "retval" if id is in the contains list.
  5567.      */
  5568.     while (item != 0)
  5569.     {
  5570.     if (item == id)
  5571.         return retval;
  5572.     if (item >= SYNID_CLUSTER)
  5573.     {
  5574.         scl_list = SYN_CLSTR(syn_buf)[item - SYNID_CLUSTER].scl_list;
  5575.         /* restrict recursiveness to 30 to avoid an endless loop for a
  5576.          * cluster that includes itself (indirectly) */
  5577.         if (scl_list != NULL && depth < 30)
  5578.         {
  5579.         ++depth;
  5580.         r = in_id_list(NULL, scl_list, ssp, contained);
  5581.         --depth;
  5582.         if (r)
  5583.             return retval;
  5584.         }
  5585.     }
  5586.     item = *++list;
  5587.     }
  5588.     return !retval;
  5589. }
  5590.  
  5591. struct subcommand
  5592. {
  5593.     char    *name;                /* subcommand name */
  5594.     void    (*func)__ARGS((exarg_T *, int));    /* function to call */
  5595. };
  5596.  
  5597. static struct subcommand subcommands[] =
  5598. {
  5599.     {"case",        syn_cmd_case},
  5600.     {"clear",        syn_cmd_clear},
  5601.     {"cluster",        syn_cmd_cluster},
  5602.     {"enable",        syn_cmd_enable},
  5603.     {"include",        syn_cmd_include},
  5604.     {"keyword",        syn_cmd_keyword},
  5605.     {"list",        syn_cmd_list},
  5606.     {"manual",        syn_cmd_manual},
  5607.     {"match",        syn_cmd_match},
  5608.     {"on",        syn_cmd_on},
  5609.     {"off",        syn_cmd_off},
  5610.     {"region",        syn_cmd_region},
  5611.     {"reset",        syn_cmd_reset},
  5612.     {"sync",        syn_cmd_sync},
  5613.     {"",        syn_cmd_list},
  5614.     {NULL, NULL}
  5615. };
  5616.  
  5617. /*
  5618.  * ":syntax".
  5619.  * This searches the subcommands[] table for the subcommand name, and calls a
  5620.  * syntax_subcommand() function to do the rest.
  5621.  */
  5622.     void
  5623. ex_syntax(eap)
  5624.     exarg_T    *eap;
  5625. {
  5626.     char_u    *arg = eap->arg;
  5627.     char_u    *subcmd_end;
  5628.     char_u    *subcmd_name;
  5629.     int        i;
  5630.  
  5631.     syn_cmdlinep = eap->cmdlinep;
  5632.  
  5633.     /* isolate subcommand name */
  5634.     for (subcmd_end = arg; ASCII_ISALPHA(*subcmd_end); ++subcmd_end)
  5635.     ;
  5636.     subcmd_name = vim_strnsave(arg, (int)(subcmd_end - arg));
  5637.     if (subcmd_name != NULL)
  5638.     {
  5639.     if (eap->skip)        /* skip error messages for all subcommands */
  5640.         ++emsg_skip;
  5641.     for (i = 0; ; ++i)
  5642.     {
  5643.         if (subcommands[i].name == NULL)
  5644.         {
  5645.         EMSG2(_("E410: Invalid :syntax subcommand: %s"), subcmd_name);
  5646.         break;
  5647.         }
  5648.         if (STRCMP(subcmd_name, (char_u *)subcommands[i].name) == 0)
  5649.         {
  5650.         eap->arg = skipwhite(subcmd_end);
  5651.         (subcommands[i].func)(eap, FALSE);
  5652.         break;
  5653.         }
  5654.     }
  5655.     vim_free(subcmd_name);
  5656.     if (eap->skip)
  5657.         --emsg_skip;
  5658.     }
  5659. }
  5660.  
  5661.     int
  5662. syntax_present(buf)
  5663.     buf_T    *buf;
  5664. {
  5665.     return (buf->b_syn_patterns.ga_len != 0
  5666.         || buf->b_syn_clusters.ga_len != 0
  5667.         || curbuf->b_keywtab != NULL
  5668.         || curbuf->b_keywtab_ic != NULL);
  5669. }
  5670.  
  5671. #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
  5672.  
  5673. static enum
  5674. {
  5675.     EXP_SUBCMD,        /* expand ":syn" sub-commands */
  5676.     EXP_CASE        /* expand ":syn case" arguments */
  5677. } expand_what;
  5678.  
  5679.  
  5680. /*
  5681.  * Handle command line completion for :syntax command.
  5682.  */
  5683.     void
  5684. set_context_in_syntax_cmd(xp, arg)
  5685.     expand_T    *xp;
  5686.     char_u    *arg;
  5687. {
  5688.     char_u    *p;
  5689.  
  5690.     /* Default: expand subcommands */
  5691.     xp->xp_context = EXPAND_SYNTAX;
  5692.     expand_what = EXP_SUBCMD;
  5693.     xp->xp_pattern = arg;
  5694.     include_link = FALSE;
  5695.     include_default = FALSE;
  5696.  
  5697.     /* (part of) subcommand already typed */
  5698.     if (*arg != NUL)
  5699.     {
  5700.     p = skiptowhite(arg);
  5701.     if (*p != NUL)            /* past first word */
  5702.     {
  5703.         xp->xp_pattern = skipwhite(p);
  5704.         if (*skiptowhite(xp->xp_pattern) != NUL)
  5705.         xp->xp_context = EXPAND_NOTHING;
  5706.         else if (STRNICMP(arg, "case", p - arg) == 0)
  5707.         expand_what = EXP_CASE;
  5708.         else if (  STRNICMP(arg, "keyword", p - arg) == 0
  5709.             || STRNICMP(arg, "region", p - arg) == 0
  5710.             || STRNICMP(arg, "match", p - arg) == 0
  5711.             || STRNICMP(arg, "list", p - arg) == 0)
  5712.         xp->xp_context = EXPAND_HIGHLIGHT;
  5713.         else
  5714.         xp->xp_context = EXPAND_NOTHING;
  5715.     }
  5716.     }
  5717. }
  5718.  
  5719. static char *(case_args[]) = {"match", "ignore", NULL};
  5720.  
  5721. /*
  5722.  * Function given to ExpandGeneric() to obtain the list syntax names for
  5723.  * expansion.
  5724.  */
  5725. /*ARGSUSED*/
  5726.     char_u *
  5727. get_syntax_name(xp, idx)
  5728.     expand_T    *xp;
  5729.     int        idx;
  5730. {
  5731.     if (expand_what == EXP_SUBCMD)
  5732.     return (char_u *)subcommands[idx].name;
  5733.     return (char_u *)case_args[idx];
  5734. }
  5735.  
  5736. #endif /* FEAT_CMDL_COMPL */
  5737.  
  5738. #if defined(FEAT_EVAL) || defined(FEAT_PRINTER) || defined(PROTO)
  5739. /*
  5740.  * Function called for expression evaluation: get syntax ID at file position.
  5741.  */
  5742.     int
  5743. syn_get_id(lnum, col, trans)
  5744.     long    lnum;
  5745.     long    col;
  5746.     int        trans;        /* remove transparancy */
  5747. {
  5748.     /* When the position is not after the current position and in the same
  5749.      * line of the same buffer, need to restart parsing. */
  5750.     if (curwin->w_buffer != syn_buf
  5751.         || lnum != current_lnum
  5752.         || col < (long)current_col)
  5753.     syntax_start(curwin, lnum);
  5754.  
  5755.     (void)get_syntax_attr((colnr_T)col);
  5756.  
  5757.     return (trans ? current_trans_id : current_id);
  5758. }
  5759. #endif
  5760.  
  5761. #if defined(FEAT_FOLDING) || defined(PROTO)
  5762. /*
  5763.  * Function called to get folding level for line "lnum" in window "wp".
  5764.  */
  5765.     int
  5766. syn_get_foldlevel(wp, lnum)
  5767.     win_T    *wp;
  5768.     long    lnum;
  5769. {
  5770.     int        level = 0;
  5771.     int        i;
  5772.  
  5773.     /* Return quickly when there are no fold items at all. */
  5774.     if (wp->w_buffer->b_syn_folditems != 0)
  5775.     {
  5776.     syntax_start(wp, lnum);
  5777.  
  5778.     for (i = 0; i < current_state.ga_len; ++i)
  5779.         if (CUR_STATE(i).si_flags & HL_FOLD)
  5780.         ++level;
  5781.     }
  5782.     if (level > wp->w_p_fdn)
  5783.     level = wp->w_p_fdn;
  5784.     return level;
  5785. }
  5786. #endif
  5787.  
  5788. #endif /* FEAT_SYN_HL */
  5789.  
  5790.  
  5791. /**************************************
  5792.  *  Highlighting stuff              *
  5793.  **************************************/
  5794.  
  5795. /*
  5796.  * The default highlight groups.  These are compiled-in for fast startup and
  5797.  * they still work when the runtime files can't be found.
  5798.  * When making changes here, also change runtime/colors/default.vim!
  5799.  */
  5800. static char *(highlight_init_both[]) =
  5801.     {
  5802. #ifdef FEAT_GUI
  5803.     "Cursor guibg=fg guifg=bg",
  5804.     "lCursor guibg=fg guifg=bg",    /* should be different, but what? */
  5805. #endif
  5806.     "ErrorMsg term=standout ctermbg=DarkRed ctermfg=White guibg=Red guifg=White",
  5807.     "IncSearch term=reverse cterm=reverse gui=reverse",
  5808.     "ModeMsg term=bold cterm=bold gui=bold",
  5809.     "NonText term=bold ctermfg=Blue gui=bold guifg=Blue",
  5810.     "StatusLine term=reverse,bold cterm=reverse,bold gui=reverse,bold",
  5811.     "StatusLineNC term=reverse cterm=reverse gui=reverse",
  5812.     "VertSplit term=reverse cterm=reverse gui=reverse",
  5813.     "Visual term=reverse cterm=reverse gui=reverse guifg=Grey guibg=fg",
  5814.     "VisualNOS term=underline,bold cterm=underline,bold gui=underline,bold",
  5815.     "DiffText term=reverse cterm=bold ctermbg=Red gui=bold guibg=Red",
  5816.     NULL
  5817.     };
  5818.  
  5819. static char *(highlight_init_light[]) =
  5820.     {
  5821.     "Directory term=bold ctermfg=DarkBlue guifg=Blue",
  5822.     "LineNr term=underline ctermfg=Brown guifg=Brown",
  5823.     "MoreMsg term=bold ctermfg=DarkGreen gui=bold guifg=SeaGreen",
  5824.     "Normal gui=NONE",
  5825.     "Question term=standout ctermfg=DarkGreen gui=bold guifg=SeaGreen",
  5826.     "Search term=reverse ctermbg=Yellow ctermfg=NONE guibg=Yellow guifg=NONE",
  5827.     "SpecialKey term=bold ctermfg=DarkBlue guifg=Blue",
  5828.     "Title term=bold ctermfg=DarkMagenta gui=bold guifg=Magenta",
  5829.     "WarningMsg term=standout ctermfg=DarkRed guifg=Red",
  5830.     "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5831.     "Folded term=standout ctermbg=Grey ctermfg=DarkBlue guibg=LightGrey guifg=DarkBlue",
  5832.     "FoldColumn term=standout ctermbg=Grey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue",
  5833.     "DiffAdd term=bold ctermbg=LightBlue guibg=LightBlue",
  5834.     "DiffChange term=bold ctermbg=LightMagenta guibg=LightMagenta",
  5835.     "DiffDelete term=bold ctermfg=Blue ctermbg=LightCyan gui=bold guifg=Blue guibg=LightCyan",
  5836.     NULL
  5837.     };
  5838.  
  5839. static char *(highlight_init_dark[]) =
  5840.     {
  5841.     "Directory term=bold ctermfg=LightCyan guifg=Cyan",
  5842.     "LineNr term=underline ctermfg=Yellow guifg=Yellow",
  5843.     "MoreMsg term=bold ctermfg=LightGreen gui=bold guifg=SeaGreen",
  5844.     "Normal gui=NONE",
  5845.     "Question term=standout ctermfg=LightGreen gui=bold guifg=Green",
  5846.     "Search term=reverse ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5847.     "SpecialKey term=bold ctermfg=LightBlue guifg=Cyan",
  5848.     "Title term=bold ctermfg=LightMagenta gui=bold guifg=Magenta",
  5849.     "WarningMsg term=standout ctermfg=LightRed guifg=Red",
  5850.     "WildMenu term=standout ctermbg=Yellow ctermfg=Black guibg=Yellow guifg=Black",
  5851.     "Folded term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=DarkGrey guifg=Cyan",
  5852.     "FoldColumn term=standout ctermbg=DarkGrey ctermfg=Cyan guibg=Grey guifg=Cyan",
  5853.     "DiffAdd term=bold ctermbg=DarkBlue guibg=DarkBlue",
  5854.     "DiffChange term=bold ctermbg=DarkMagenta guibg=DarkMagenta",
  5855.     "DiffDelete term=bold ctermfg=Blue ctermbg=DarkCyan gui=bold guifg=Blue guibg=DarkCyan",
  5856.     NULL
  5857.     };
  5858.  
  5859.     void
  5860. init_highlight(both, reset)
  5861.     int        both;        /* include groups where 'bg' doesn't matter */
  5862.     int        reset;        /* clear group first */
  5863. {
  5864.     int        i;
  5865.     char    **pp;
  5866.     static int    had_both = FALSE;
  5867. #ifdef FEAT_EVAL
  5868.     char_u    *p;
  5869.  
  5870.     /*
  5871.      * Try finding the color scheme file.  Used when a color file was loaded
  5872.      * and 'background' or 't_Co' is changed.
  5873.      */
  5874.     p = get_var_value((char_u *)"colors_name");
  5875.     if (p != NULL && load_colors(p) == OK)
  5876.     return;
  5877. #endif
  5878.  
  5879.     /*
  5880.      * Didn't use a color file, use the compiled-in colors.
  5881.      */
  5882.     if (both)
  5883.     {
  5884.     had_both = TRUE;
  5885.     pp = highlight_init_both;
  5886.     for (i = 0; pp[i] != NULL; ++i)
  5887.         do_highlight((char_u *)pp[i], reset, TRUE);
  5888.     }
  5889.     else if (!had_both)
  5890.     /* Don't do anything before the call with both == TRUE from main().
  5891.      * Not everything has been setup then, and that call will overrule
  5892.      * everything anyway. */
  5893.     return;
  5894.  
  5895.     if (TO_LOWER(*p_bg) == 'l')
  5896.     pp = highlight_init_light;
  5897.     else
  5898.     pp = highlight_init_dark;
  5899.     for (i = 0; pp[i] != NULL; ++i)
  5900.     do_highlight((char_u *)pp[i], reset, TRUE);
  5901.  
  5902. #ifdef FEAT_SYN_HL
  5903.     /*
  5904.      * If syntax highlighting is enabled load the highlighting for it.
  5905.      */
  5906.     if (get_var_value((char_u *)"syntax_on") != NULL)
  5907.     (void)cmd_runtime((char_u *)"syntax/syncolor.vim", TRUE);
  5908. #endif
  5909. }
  5910.  
  5911. /*
  5912.  * Load color file "p".
  5913.  * Return OK for success, FAIL for failure.
  5914.  */
  5915.     int
  5916. load_colors(p)
  5917.     char_u    *p;
  5918. {
  5919.     char_u    *buf;
  5920.     int        retval = FAIL;
  5921.     static int    recursive = FALSE;
  5922.  
  5923.     /* When being called recursively, this is probably because setting
  5924.      * 'background' caused the highlighting to be reloaded.  This means it is
  5925.      * working, thus we should return OK. */
  5926.     if (recursive)
  5927.     return OK;
  5928.  
  5929.     recursive = TRUE;
  5930.     buf = alloc((unsigned)(STRLEN(p) + 12));
  5931.     if (buf != NULL)
  5932.     {
  5933.     sprintf((char *)buf, "colors/%s.vim", p);
  5934.     retval = cmd_runtime(buf, FALSE);
  5935.     vim_free(buf);
  5936.     }
  5937.     recursive = FALSE;
  5938.  
  5939.     return retval;
  5940. }
  5941.  
  5942. /*
  5943.  * Handle the ":highlight .." command.
  5944.  * When using ":hi clear" this is called recursively for each group with
  5945.  * "forceit" and "init" both TRUE.
  5946.  */
  5947.     void
  5948. do_highlight(line, forceit, init)
  5949.     char_u    *line;
  5950.     int        forceit;
  5951.     int        init;        /* TRUE when called for initializing */
  5952. {
  5953.     char_u    *name_end;
  5954.     char_u    *p;
  5955.     char_u    *linep;
  5956.     char_u    *key_start;
  5957.     char_u    *arg_start;
  5958.     char_u    *key = NULL, *arg = NULL;
  5959.     long    i;
  5960.     int        off;
  5961.     int        len;
  5962.     int        attr;
  5963.     int        id;
  5964.     int        idx;
  5965.     int        dodefault = FALSE;
  5966.     int        doclear = FALSE;
  5967.     int        dolink = FALSE;
  5968.     int        error = FALSE;
  5969.     int        color;
  5970.     int        is_normal_group = FALSE;    /* "Normal" group */
  5971. #ifdef FEAT_GUI_X11
  5972.     int        is_menu_group = FALSE;        /* "Menu" group */
  5973.     int        is_scrollbar_group = FALSE;    /* "Scrollbar" group */
  5974.     int        is_tooltip_group = FALSE;    /* "Tooltip" group */
  5975.     int        do_colors = FALSE;        /* need to update colors? */
  5976. #else
  5977. # define is_menu_group 0
  5978. # define is_tooltip_group 0
  5979. #endif
  5980.  
  5981.     /*
  5982.      * If no argument, list current highlighting.
  5983.      */
  5984.     if (ends_excmd(*line))
  5985.     {
  5986.     for (i = 1; i <= highlight_ga.ga_len && !got_int; ++i)
  5987.         /* TODO: only call when the group has attributes set */
  5988.         highlight_list_one((int)i);
  5989.     return;
  5990.     }
  5991.  
  5992.     /*
  5993.      * Isolate the name.
  5994.      */
  5995.     name_end = skiptowhite(line);
  5996.     linep = skipwhite(name_end);
  5997.  
  5998.     /*
  5999.      * Check for "default" argument.
  6000.      */
  6001.     if (STRNCMP(line, "default", name_end - line) == 0)
  6002.     {
  6003.     dodefault = TRUE;
  6004.     line = linep;
  6005.     name_end = skiptowhite(line);
  6006.     linep = skipwhite(name_end);
  6007.     }
  6008.  
  6009.     /*
  6010.      * Check for "clear" or "link" argument.
  6011.      */
  6012.     if (STRNCMP(line, "clear", name_end - line) == 0)
  6013.     doclear = TRUE;
  6014.     if (STRNCMP(line, "link", name_end - line) == 0)
  6015.     dolink = TRUE;
  6016.  
  6017.     /*
  6018.      * ":highlight {group-name}": list highlighting for one group.
  6019.      */
  6020.     if (!doclear && !dolink && ends_excmd(*linep))
  6021.     {
  6022.     id = syn_namen2id(line, (int)(name_end - line));
  6023.     if (id == 0)
  6024.         EMSG2(_("E411: highlight group not found: %s"), line);
  6025.     else
  6026.         highlight_list_one(id);
  6027.     return;
  6028.     }
  6029.  
  6030.     /*
  6031.      * Handle ":highlight link {from} {to}" command.
  6032.      */
  6033.     if (dolink)
  6034.     {
  6035.     char_u        *from_start = linep;
  6036.     char_u        *from_end;
  6037.     char_u        *to_start;
  6038.     char_u        *to_end;
  6039.     int        from_id;
  6040.     int        to_id;
  6041.  
  6042.     from_end = skiptowhite(from_start);
  6043.     to_start = skipwhite(from_end);
  6044.     to_end     = skiptowhite(to_start);
  6045.  
  6046.     if (ends_excmd(*from_start) || ends_excmd(*to_start))
  6047.     {
  6048.         EMSG2(_("E412: Not enough arguments: \":highlight link %s\""),
  6049.                                   from_start);
  6050.         return;
  6051.     }
  6052.  
  6053.     if (!ends_excmd(*skipwhite(to_end)))
  6054.     {
  6055.         EMSG2(_("E413: Too many arguments: \":highlight link %s\""), from_start);
  6056.         return;
  6057.     }
  6058.  
  6059.     from_id = syn_check_group(from_start, (int)(from_end - from_start));
  6060.     if (STRNCMP(to_start, "NONE", 4) == 0)
  6061.         to_id = 0;
  6062.     else
  6063.         to_id = syn_check_group(to_start, (int)(to_end - to_start));
  6064.  
  6065.     if (from_id > 0 && (!init || HL_TABLE()[from_id - 1].sg_set == 0))
  6066.     {
  6067.         /*
  6068.          * Don't allow a link when there already is some highlighting
  6069.          * for the group, unless '!' is used
  6070.          */
  6071.         if (to_id > 0 && !forceit && !init
  6072.                    && hl_has_settings(from_id - 1, dodefault))
  6073.         {
  6074.         if (sourcing_name == NULL && !dodefault)
  6075.             EMSG(_("E414: group has settings, highlight link ignored"));
  6076.         }
  6077.         else
  6078.         {
  6079.         if (!init)
  6080.             HL_TABLE()[from_id - 1].sg_set |= SG_LINK;
  6081.         HL_TABLE()[from_id - 1].sg_link = to_id;
  6082.         redraw_all_later(NOT_VALID);
  6083.         }
  6084.     }
  6085.  
  6086.     /* Only call highlight_changed() once, after sourcing a syntax file */
  6087.     need_highlight_changed = TRUE;
  6088.  
  6089.     return;
  6090.     }
  6091.  
  6092.     if (doclear)
  6093.     {
  6094.     /*
  6095.      * ":highlight clear [group]" command.
  6096.      */
  6097.     line = linep;
  6098.     if (ends_excmd(*line))
  6099.     {
  6100. #ifdef FEAT_GUI
  6101.         /* First, we do not destroy the old values, but allocate the new
  6102.          * ones and update the display. THEN we destroy the old values.
  6103.          * If we destroy the old values first, then the old values
  6104.          * (such as GuiFont's or GuiFontset's) will still be displayed but
  6105.          * invalid because they were free'd.
  6106.          */
  6107.         if (gui.in_use)
  6108.         {
  6109. # ifdef FEAT_BEVAL
  6110.         gui_init_tooltip_font();
  6111. # endif
  6112. # if defined(FEAT_MENU) && (defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MOTIF))
  6113.         gui_init_menu_font();
  6114. # endif
  6115.         }
  6116. # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_X11)
  6117.         gui_mch_def_colors();
  6118. # endif
  6119. # ifdef FEAT_GUI_X11
  6120. #  ifdef FEAT_MENU
  6121.  
  6122.         /* This only needs to be done when there is no Menu highlight
  6123.          * group defined by default, which IS currently the case.
  6124.          */
  6125.         gui_mch_new_menu_colors();
  6126. #  endif
  6127.         if (gui.in_use)
  6128.         {
  6129.         gui_new_scrollbar_colors();
  6130. #  ifdef FEAT_BEVAL
  6131.         gui_mch_new_tooltip_colors();
  6132. #  endif
  6133. #  ifdef FEAT_MENU
  6134.         gui_mch_new_menu_font();
  6135. #  endif
  6136.         }
  6137. # endif
  6138.  
  6139.         /* Ok, we're done allocating the new default graphics items.
  6140.          * The screen should already be refreshed at this point.
  6141.          * It is now Ok to clear out the old data.
  6142.          */
  6143. #endif
  6144. #ifdef FEAT_EVAL
  6145.         do_unlet((char_u *)"colors_name");
  6146. #endif
  6147.         restore_cterm_colors();
  6148.  
  6149.         /*
  6150.          * Clear all default highlight groups and load the defaults.
  6151.          */
  6152.         for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  6153.         highlight_clear(idx);
  6154.         init_highlight(TRUE, TRUE);
  6155. #ifdef FEAT_GUI
  6156.         if (gui.in_use)
  6157.         highlight_gui_started();
  6158. #endif
  6159.         highlight_changed();
  6160.         redraw_later_clear();
  6161.         return;
  6162.     }
  6163.     name_end = skiptowhite(line);
  6164.     linep = skipwhite(name_end);
  6165.     }
  6166.  
  6167.     /*
  6168.      * Find the group name in the table.  If it does not exist yet, add it.
  6169.      */
  6170.     id = syn_check_group(line, (int)(name_end - line));
  6171.     if (id == 0)            /* failed (out of memory) */
  6172.     return;
  6173.     idx = id - 1;            /* index is ID minus one */
  6174.  
  6175.     /* Return if "default" was used and the group already has settings. */
  6176.     if (dodefault && hl_has_settings(idx, TRUE))
  6177.     return;
  6178.  
  6179.     if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
  6180.     is_normal_group = TRUE;
  6181. #ifdef FEAT_GUI_X11
  6182.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "MENU") == 0)
  6183.     is_menu_group = TRUE;
  6184.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "SCROLLBAR") == 0)
  6185.     is_scrollbar_group = TRUE;
  6186.     else if (STRCMP(HL_TABLE()[idx].sg_name_u, "TOOLTIP") == 0)
  6187.     is_tooltip_group = TRUE;
  6188. #endif
  6189.  
  6190.     /* Clear the highlighting for ":hi clear {group}" and ":hi clear". */
  6191.     if (doclear || (forceit && init))
  6192.     {
  6193.     highlight_clear(idx);
  6194.     if (!doclear)
  6195.         HL_TABLE()[idx].sg_set = 0;
  6196.     }
  6197.  
  6198.     if (!doclear)
  6199.       while (!ends_excmd(*linep))
  6200.       {
  6201.     key_start = linep;
  6202.     if (*linep == '=')
  6203.     {
  6204.         EMSG2(_("E415: unexpected equal sign: %s"), key_start);
  6205.         error = TRUE;
  6206.         break;
  6207.     }
  6208.  
  6209.     /*
  6210.      * Isolate the key ("term", "ctermfg", "ctermbg", "font", "guifg" or
  6211.      * "guibg").
  6212.      */
  6213.     while (*linep && !vim_iswhite(*linep) && *linep != '=')
  6214.         ++linep;
  6215.     vim_free(key);
  6216.     key = vim_strnsave_up(key_start, (int)(linep - key_start));
  6217.     if (key == NULL)
  6218.     {
  6219.         error = TRUE;
  6220.         break;
  6221.     }
  6222.     linep = skipwhite(linep);
  6223.  
  6224.     if (STRCMP(key, "NONE") == 0)
  6225.     {
  6226.         if (!init || HL_TABLE()[idx].sg_set == 0)
  6227.         {
  6228.         if (!init)
  6229.             HL_TABLE()[idx].sg_set |= SG_TERM+SG_CTERM+SG_GUI;
  6230.         highlight_clear(idx);
  6231.         }
  6232.         continue;
  6233.     }
  6234.  
  6235.     /*
  6236.      * Check for the equal sign.
  6237.      */
  6238.     if (*linep != '=')
  6239.     {
  6240.         EMSG2(_("E416: missing equal sign: %s"), key_start);
  6241.         error = TRUE;
  6242.         break;
  6243.     }
  6244.     ++linep;
  6245.  
  6246.     /*
  6247.      * Isolate the argument.
  6248.      */
  6249.     linep = skipwhite(linep);
  6250.     if (*linep == '\'')        /* guifg='color name' */
  6251.     {
  6252.         arg_start = ++linep;
  6253.         linep = vim_strchr(linep, '\'');
  6254.     }
  6255.     else
  6256.     {
  6257.         arg_start = linep;
  6258.         linep = skiptowhite(linep);
  6259.     }
  6260.     if (linep == arg_start)
  6261.     {
  6262.         EMSG2(_("E417: missing argument: %s"), key_start);
  6263.         error = TRUE;
  6264.         break;
  6265.     }
  6266.     vim_free(arg);
  6267.     arg = vim_strnsave(arg_start, (int)(linep - arg_start));
  6268.     if (arg == NULL)
  6269.     {
  6270.         error = TRUE;
  6271.         break;
  6272.     }
  6273.     if (*linep == '\'')
  6274.         ++linep;
  6275.  
  6276.     /*
  6277.      * Store the argument.
  6278.      */
  6279.     if (  STRCMP(key, "TERM") == 0
  6280.         || STRCMP(key, "CTERM") == 0
  6281.         || STRCMP(key, "GUI") == 0)
  6282.     {
  6283.         attr = 0;
  6284.         off = 0;
  6285.         while (arg[off] != NUL)
  6286.         {
  6287.         for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; )
  6288.         {
  6289.             len = (int)STRLEN(hl_name_table[i]);
  6290.             if (STRNICMP(arg + off, hl_name_table[i], len) == 0)
  6291.             {
  6292.             attr |= hl_attr_table[i];
  6293.             off += len;
  6294.             break;
  6295.             }
  6296.         }
  6297.         if (i < 0)
  6298.         {
  6299.             EMSG2(_("E418: Illegal value: %s"), arg);
  6300.             error = TRUE;
  6301.             break;
  6302.         }
  6303.         if (arg[off] == ',')        /* another one follows */
  6304.             ++off;
  6305.         }
  6306.         if (error)
  6307.         break;
  6308.         if (*key == 'T')
  6309.         {
  6310.         if (!init || !(HL_TABLE()[idx].sg_set & SG_TERM))
  6311.         {
  6312.             if (!init)
  6313.             HL_TABLE()[idx].sg_set |= SG_TERM;
  6314.             HL_TABLE()[idx].sg_term = attr;
  6315.         }
  6316.         }
  6317.         else if (*key == 'C')
  6318.         {
  6319.         if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
  6320.         {
  6321.             if (!init)
  6322.             HL_TABLE()[idx].sg_set |= SG_CTERM;
  6323.             HL_TABLE()[idx].sg_cterm = attr;
  6324.             HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6325.         }
  6326.         }
  6327. #ifdef FEAT_GUI
  6328.         else
  6329.         {
  6330.         if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6331.         {
  6332.             if (!init)
  6333.             HL_TABLE()[idx].sg_set |= SG_GUI;
  6334.             HL_TABLE()[idx].sg_gui = attr;
  6335.         }
  6336.         }
  6337. #endif
  6338.     }
  6339.     else if (STRCMP(key, "FONT") == 0)
  6340.     {
  6341.         /* in non-GUI fonts are simply ignored */
  6342. #ifdef FEAT_GUI
  6343.         if (!gui.shell_created)
  6344.         {
  6345.         /* GUI not started yet, always accept the name. */
  6346.         vim_free(HL_TABLE()[idx].sg_font_name);
  6347.         HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6348.         }
  6349.         else
  6350.         {
  6351.         GuiFont temp_sg_font = HL_TABLE()[idx].sg_font;
  6352. # ifdef FEAT_XFONTSET
  6353.         GuiFontset temp_sg_fontset = HL_TABLE()[idx].sg_fontset;
  6354. # endif
  6355.         /* First, save the current font/fontset.
  6356.          * Then try to allocate the font/fontset.
  6357.          * If the allocation fails, HL_TABLE()[idx].sg_font OR
  6358.          * sg_fontset will be set to NOFONT or NOFONTSET respectively.
  6359.          */
  6360.  
  6361.         HL_TABLE()[idx].sg_font = NOFONT;
  6362. # ifdef FEAT_XFONTSET
  6363.         HL_TABLE()[idx].sg_fontset = NOFONTSET;
  6364. # endif
  6365.         hl_do_font(idx, arg, is_normal_group, is_menu_group,
  6366.                                 is_tooltip_group);
  6367.  
  6368. # ifdef FEAT_XFONTSET
  6369.         if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
  6370.         {
  6371.             /* New fontset was accepted. Free the old one, if there was
  6372.              * one.
  6373.              */
  6374.             gui_mch_free_fontset(temp_sg_fontset);
  6375.             vim_free(HL_TABLE()[idx].sg_font_name);
  6376.             HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6377.         }
  6378.         else
  6379.             HL_TABLE()[idx].sg_fontset = temp_sg_fontset;
  6380. # endif
  6381.         if (HL_TABLE()[idx].sg_font != NOFONT)
  6382.         {
  6383.             /* New font was accepted. Free the old one, if there was
  6384.              * one.
  6385.              */
  6386.             gui_mch_free_font(temp_sg_font);
  6387.             vim_free(HL_TABLE()[idx].sg_font_name);
  6388.             HL_TABLE()[idx].sg_font_name = vim_strsave(arg);
  6389.         }
  6390.         else
  6391.             HL_TABLE()[idx].sg_font = temp_sg_font;
  6392.         }
  6393. #endif
  6394.     }
  6395.     else if (STRCMP(key, "CTERMFG") == 0 || STRCMP(key, "CTERMBG") == 0)
  6396.     {
  6397.       if (!init || !(HL_TABLE()[idx].sg_set & SG_CTERM))
  6398.       {
  6399.         if (!init)
  6400.         HL_TABLE()[idx].sg_set |= SG_CTERM;
  6401.  
  6402.         /* When setting the foreground color, and previously the "bold"
  6403.          * flag was set for a light color, reset it now */
  6404.         if (key[5] == 'F' && HL_TABLE()[idx].sg_cterm_bold)
  6405.         {
  6406.         HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
  6407.         HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6408.         }
  6409.  
  6410.         if (isdigit(*arg))
  6411.         color = atoi((char *)arg);
  6412.         else if (STRICMP(arg, "fg") == 0)
  6413.         {
  6414.         if (cterm_normal_fg_color)
  6415.             color = cterm_normal_fg_color - 1;
  6416.         else
  6417.         {
  6418.             EMSG(_("E419: FG color unknown"));
  6419.             error = TRUE;
  6420.             break;
  6421.         }
  6422.         }
  6423.         else if (STRICMP(arg, "bg") == 0)
  6424.         {
  6425.         if (cterm_normal_bg_color > 0)
  6426.             color = cterm_normal_bg_color - 1;
  6427.         else
  6428.         {
  6429.             EMSG(_("E420: BG color unknown"));
  6430.             error = TRUE;
  6431.             break;
  6432.         }
  6433.         }
  6434.         else
  6435.         {
  6436.         static char *(color_names[28]) = {
  6437.                 "Black", "DarkBlue", "DarkGreen", "DarkCyan",
  6438.                 "DarkRed", "DarkMagenta", "Brown", "DarkYellow",
  6439.                 "Gray", "Grey",
  6440.                 "LightGray", "LightGrey", "DarkGray", "DarkGrey",
  6441.                 "Blue", "LightBlue", "Green", "LightGreen",
  6442.                 "Cyan", "LightCyan", "Red", "LightRed", "Magenta",
  6443.                 "LightMagenta", "Yellow", "LightYellow", "White", "NONE"};
  6444.         static int color_numbers_16[28] = {0, 1, 2, 3,
  6445.                          4, 5, 6, 6,
  6446.                          7, 7,
  6447.                          7, 7, 8, 8,
  6448.                          9, 9, 10, 10,
  6449.                          11, 11, 12, 12, 13,
  6450.                          13, 14, 14, 15, -1};
  6451.         /* for xterm with 88 colors... */
  6452.         static int color_numbers_88[28] = {0, 4, 2, 6,
  6453.                          1, 5, 32, 72,
  6454.                          84, 84,
  6455.                          7, 7, 82, 82,
  6456.                          12, 43, 10, 61,
  6457.                          14, 63, 9, 74, 13,
  6458.                          75, 11, 78, 15, -1};
  6459.         /* for xterm with 256 colors... */
  6460.         static int color_numbers_256[28] = {0, 4, 2, 6,
  6461.                          1, 5, 130, 130,
  6462.                          248, 248,
  6463.                          7, 7, 242, 242,
  6464.                          12, 81, 10, 121,
  6465.                          14, 159, 9, 224, 13,
  6466.                          225, 11, 229, 15, -1};
  6467.         /* for terminals with less than 16 colors... */
  6468.         static int color_numbers_8[28] = {0, 4, 2, 6,
  6469.                          1, 5, 3, 3,
  6470.                          7, 7,
  6471.                          7, 7, 0+8, 0+8,
  6472.                          4+8, 4+8, 2+8, 2+8,
  6473.                          6+8, 6+8, 1+8, 1+8, 5+8,
  6474.                          5+8, 3+8, 3+8, 7+8, -1};
  6475.  
  6476.         /* reduce calls to STRICMP a bit, it can be slow */
  6477.         off = TO_UPPER(*arg);
  6478.         for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
  6479.             if (off == color_names[i][0]
  6480.                  && STRICMP(arg + 1, color_names[i] + 1) == 0)
  6481.             break;
  6482.         if (i < 0)
  6483.         {
  6484.             EMSG2(_("E421: Color name or number not recognized: %s"), key_start);
  6485.             error = TRUE;
  6486.             break;
  6487.         }
  6488.  
  6489.         /* Use the _16 table to check if its a valid color name. */
  6490.         color = color_numbers_16[i];
  6491.         if (color >= 0)
  6492.         {
  6493.             if (t_colors == 8)
  6494.             {
  6495.             /* t_Co is 8: use the 8 colors table */
  6496.             color = color_numbers_8[i];
  6497.             if (key[5] == 'F')
  6498.             {
  6499.                 /* set/reset bold attribute to get light foreground
  6500.                  * colors (on some terminals, e.g. "linux") */
  6501.                 if (color & 8)
  6502.                 {
  6503.                 HL_TABLE()[idx].sg_cterm |= HL_BOLD;
  6504.                 HL_TABLE()[idx].sg_cterm_bold = TRUE;
  6505.                 }
  6506.                 else
  6507.                 HL_TABLE()[idx].sg_cterm &= ~HL_BOLD;
  6508.             }
  6509.             color &= 7;    /* truncate to 8 colors */
  6510.             }
  6511.             else if (t_colors == 16 || t_colors == 88
  6512.                                || t_colors == 256)
  6513.             {
  6514.             /*
  6515.              * Guess: if the termcap entry ends in 'm', it is
  6516.              * probably an xterm-like terminal.  Use the changed
  6517.              * order for colors.
  6518.              */
  6519.             if (*T_CAF != NUL)
  6520.                 p = T_CAF;
  6521.             else
  6522.                 p = T_CSF;
  6523.             if (*p != NUL && *(p + STRLEN(p) - 1) == 'm')
  6524.                 switch (t_colors)
  6525.                 {
  6526.                 case 16:
  6527.                     color = color_numbers_8[i];
  6528.                     break;
  6529.                 case 88:
  6530.                     color = color_numbers_88[i];
  6531.                     break;
  6532.                 case 256:
  6533.                     color = color_numbers_256[i];
  6534.                     break;
  6535.                 }
  6536.             }
  6537.         }
  6538.         }
  6539.         /* Add one to the argument, to avoid zero */
  6540.         if (key[5] == 'F')
  6541.         {
  6542.         HL_TABLE()[idx].sg_cterm_fg = color + 1;
  6543.         if (is_normal_group)
  6544.         {
  6545.             cterm_normal_fg_color = color + 1;
  6546.             cterm_normal_fg_bold = (HL_TABLE()[idx].sg_cterm & HL_BOLD);
  6547. #ifdef FEAT_GUI
  6548.             /* Don't do this if the GUI is used. */
  6549.             if (!gui.in_use && !gui.starting)
  6550. #endif
  6551.             {
  6552.             must_redraw = CLEAR;
  6553.             if (termcap_active)
  6554.                 term_fg_color(color);
  6555.             }
  6556.         }
  6557.         }
  6558.         else
  6559.         {
  6560.         HL_TABLE()[idx].sg_cterm_bg = color + 1;
  6561.         if (is_normal_group)
  6562.         {
  6563.             cterm_normal_bg_color = color + 1;
  6564. #ifdef FEAT_GUI
  6565.             /* Don't mess with 'background' if the GUI is used. */
  6566.             if (!gui.in_use && !gui.starting)
  6567. #endif
  6568.             {
  6569.             must_redraw = CLEAR;
  6570.             if (termcap_active)
  6571.                 term_bg_color(color);
  6572.             if (t_colors < 16)
  6573.                 i = (color == 0 || color == 4);
  6574.             else
  6575.                 i = (color < 7 || color == 8);
  6576.             /* Set the 'background' option if the value is wrong. */
  6577.             if (i != (*p_bg == 'd'))
  6578.                 set_option_value((char_u *)"bg", 0L,
  6579.                  i ? (char_u *)"dark" : (char_u *)"light", 0);
  6580.             }
  6581.         }
  6582.         }
  6583.       }
  6584.     }
  6585.     else if (STRCMP(key, "GUIFG") == 0)
  6586.     {
  6587. #ifdef FEAT_GUI        /* in non-GUI guifg colors are simply ignored */
  6588.       if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6589.       {
  6590.         if (!init)
  6591.         HL_TABLE()[idx].sg_set |= SG_GUI;
  6592.  
  6593.         /* Add one to the argument, to avoid zero */
  6594.         i = color_name2handle(arg) + 1;
  6595.         if (i > 0 || STRCMP(arg, "NONE") == 0 || !gui.in_use)
  6596.         {
  6597.         HL_TABLE()[idx].sg_gui_fg = i;
  6598.         vim_free(HL_TABLE()[idx].sg_gui_fg_name);
  6599.         if (STRCMP(arg, "NONE"))
  6600.             HL_TABLE()[idx].sg_gui_fg_name = vim_strsave(arg);
  6601.         else
  6602.             HL_TABLE()[idx].sg_gui_fg_name = NULL;
  6603. # ifdef FEAT_GUI_X11
  6604.         if (is_menu_group)
  6605.             gui.menu_fg_pixel = i - 1;
  6606.         if (is_scrollbar_group)
  6607.             gui.scroll_fg_pixel = i - 1;
  6608. #  ifdef FEAT_BEVAL
  6609.         if (is_tooltip_group)
  6610.             gui.tooltip_fg_pixel = i - 1;
  6611. #  endif
  6612.         do_colors = TRUE;
  6613. # endif
  6614.         }
  6615.       }
  6616. #endif
  6617.     }
  6618.     else if (STRCMP(key, "GUIBG") == 0)
  6619.     {
  6620. #ifdef FEAT_GUI        /* in non-GUI guibg colors are simply ignored */
  6621.       if (!init || !(HL_TABLE()[idx].sg_set & SG_GUI))
  6622.       {
  6623.         if (!init)
  6624.         HL_TABLE()[idx].sg_set |= SG_GUI;
  6625.  
  6626.         /* Add one to the argument, to avoid zero */
  6627.         i = color_name2handle(arg) + 1;
  6628.         if (i > 0 || STRCMP(arg, "NONE") == 0 || !gui.in_use)
  6629.         {
  6630.         HL_TABLE()[idx].sg_gui_bg = i;
  6631.         vim_free(HL_TABLE()[idx].sg_gui_bg_name);
  6632.         if (STRCMP(arg, "NONE"))
  6633.             HL_TABLE()[idx].sg_gui_bg_name = vim_strsave(arg);
  6634.         else
  6635.             HL_TABLE()[idx].sg_gui_bg_name = NULL;
  6636. # ifdef FEAT_GUI_X11
  6637.         if (is_menu_group)
  6638.             gui.menu_bg_pixel = i - 1;
  6639.         if (is_scrollbar_group)
  6640.             gui.scroll_bg_pixel = i - 1;
  6641. #  ifdef FEAT_BEVAL
  6642.         if (is_tooltip_group)
  6643.             gui.tooltip_bg_pixel = i - 1;
  6644. #  endif
  6645.         do_colors = TRUE;
  6646. # endif
  6647.         }
  6648.       }
  6649. #endif
  6650.     }
  6651.     else if (STRCMP(key, "START") == 0 || STRCMP(key, "STOP") == 0)
  6652.     {
  6653.         char_u    buf[100];
  6654.         char_u    *tname;
  6655.  
  6656.         if (!init)
  6657.         HL_TABLE()[idx].sg_set |= SG_TERM;
  6658.  
  6659.         /*
  6660.          * The "start" and "stop"  arguments can be a literal escape
  6661.          * sequence, or a comma seperated list of terminal codes.
  6662.          */
  6663.         if (STRNCMP(arg, "t_", 2) == 0)
  6664.         {
  6665.         off = 0;
  6666.         buf[0] = 0;
  6667.         while (arg[off] != NUL)
  6668.         {
  6669.             /* Isolate one termcap name */
  6670.             for (len = 0; arg[off + len] &&
  6671.                          arg[off + len] != ','; ++len)
  6672.             ;
  6673.             tname = vim_strnsave(arg + off, len);
  6674.             if (tname == NULL)        /* out of memory */
  6675.             {
  6676.             error = TRUE;
  6677.             break;
  6678.             }
  6679.             /* lookup the escape sequence for the item */
  6680.             p = get_term_code(tname);
  6681.             vim_free(tname);
  6682.             if (p == NULL)        /* ignore non-existing things */
  6683.             p = (char_u *)"";
  6684.  
  6685.             /* Append it to the already found stuff */
  6686.             if ((int)(STRLEN(buf) + STRLEN(p)) >= 99)
  6687.             {
  6688.             EMSG2(_("E422: terminal code too long: %s"), arg);
  6689.             error = TRUE;
  6690.             break;
  6691.             }
  6692.             STRCAT(buf, p);
  6693.  
  6694.             /* Advance to the next item */
  6695.             off += len;
  6696.             if (arg[off] == ',')        /* another one follows */
  6697.             ++off;
  6698.         }
  6699.         }
  6700.         else
  6701.         {
  6702.         /*
  6703.          * Copy characters from arg[] to buf[], translating <> codes.
  6704.          */
  6705.         for (p = arg, off = 0; off < 100 && *p; )
  6706.         {
  6707.             len = trans_special(&p, buf + off, FALSE);
  6708.             if (len)            /* recognized special char */
  6709.             off += len;
  6710.             else            /* copy as normal char */
  6711.             buf[off++] = *p++;
  6712.         }
  6713.         buf[off] = NUL;
  6714.         }
  6715.         if (error)
  6716.         break;
  6717.  
  6718.         if (STRCMP(buf, "NONE") == 0)    /* resetting the value */
  6719.         p = NULL;
  6720.         else
  6721.         p = vim_strsave(buf);
  6722.         if (key[2] == 'A')
  6723.         {
  6724.         vim_free(HL_TABLE()[idx].sg_start);
  6725.         HL_TABLE()[idx].sg_start = p;
  6726.         }
  6727.         else
  6728.         {
  6729.         vim_free(HL_TABLE()[idx].sg_stop);
  6730.         HL_TABLE()[idx].sg_stop = p;
  6731.         }
  6732.     }
  6733.     else
  6734.     {
  6735.         EMSG2(_("E423: Illegal argument: %s"), key_start);
  6736.         error = TRUE;
  6737.         break;
  6738.     }
  6739.  
  6740.     /*
  6741.      * When highlighting has been given for a group, don't link it.
  6742.      */
  6743.     if (!init || !(HL_TABLE()[idx].sg_set & SG_LINK))
  6744.         HL_TABLE()[idx].sg_link = 0;
  6745.  
  6746.     /*
  6747.      * Continue with next argument.
  6748.      */
  6749.     linep = skipwhite(linep);
  6750.       }
  6751.  
  6752.     /*
  6753.      * If there is an error, and it's a new entry, remove it from the table.
  6754.      */
  6755.     if (error && idx == highlight_ga.ga_len)
  6756.     syn_unadd_group();
  6757.     else
  6758.     {
  6759.     if (is_normal_group)
  6760.     {
  6761.         HL_TABLE()[idx].sg_term_attr = 0;
  6762.         HL_TABLE()[idx].sg_cterm_attr = 0;
  6763. #ifdef FEAT_GUI
  6764.         HL_TABLE()[idx].sg_gui_attr = 0;
  6765.         /*
  6766.          * Need to update all groups, because they might be using "bg"
  6767.          * and/or "fg", which have been changed now.
  6768.          */
  6769.         if (gui.in_use)
  6770.         highlight_gui_started();
  6771. #endif
  6772.     }
  6773. #ifdef FEAT_GUI_X11
  6774. # ifdef FEAT_MENU
  6775.     else if (is_menu_group)
  6776.     {
  6777.         if (gui.in_use && do_colors)
  6778.         gui_mch_new_menu_colors();
  6779.     }
  6780. # endif
  6781.     else if (is_scrollbar_group)
  6782.     {
  6783.         if (gui.in_use && do_colors)
  6784.         gui_new_scrollbar_colors();
  6785.     }
  6786. # ifdef FEAT_BEVAL
  6787.     else if (is_tooltip_group)
  6788.     {
  6789.         if (gui.in_use && do_colors)
  6790.         gui_mch_new_tooltip_colors();
  6791.     }
  6792. # endif
  6793. #endif
  6794.     else
  6795.         set_hl_attr(idx);
  6796.     redraw_all_later(NOT_VALID);
  6797.     }
  6798.     vim_free(key);
  6799.     vim_free(arg);
  6800.  
  6801.     /* Only call highlight_changed() once, after sourcing a syntax file */
  6802.     need_highlight_changed = TRUE;
  6803. }
  6804.  
  6805. /*
  6806.  * Reset the cterm colors to what they were before Vim was started, if
  6807.  * possible.  Otherwise reset them to zero.
  6808.  */
  6809.     void
  6810. restore_cterm_colors()
  6811. {
  6812. #if defined(MSDOS) || (defined(WIN3264) && !defined(FEAT_GUI_W32))
  6813.     /* Since t_me has been set, this probably means that the user
  6814.      * wants to use this as default colors.  Need to reset default
  6815.      * background/foreground colors. */
  6816.     mch_set_normal_colors();
  6817. #else
  6818.     cterm_normal_fg_color = 0;
  6819.     cterm_normal_fg_bold = 0;
  6820.     cterm_normal_bg_color = 0;
  6821. #endif
  6822. }
  6823.  
  6824. /*
  6825.  * Return TRUE if highlight group "idx" has any settings.
  6826.  * When "check_link" is TRUE also check for an existing link.
  6827.  */
  6828.     static int
  6829. hl_has_settings(idx, check_link)
  6830.     int        idx;
  6831.     int        check_link;
  6832. {
  6833.     return (   HL_TABLE()[idx].sg_term_attr != 0
  6834.         || HL_TABLE()[idx].sg_cterm_attr != 0
  6835. #ifdef FEAT_GUI
  6836.         || HL_TABLE()[idx].sg_gui_attr != 0
  6837. #endif
  6838.         || (check_link && (HL_TABLE()[idx].sg_set & SG_LINK)));
  6839. }
  6840.  
  6841. /*
  6842.  * Clear highlighting for one group.
  6843.  */
  6844.     static void
  6845. highlight_clear(idx)
  6846.     int idx;
  6847. {
  6848.     HL_TABLE()[idx].sg_term = 0;
  6849.     vim_free(HL_TABLE()[idx].sg_start);
  6850.     HL_TABLE()[idx].sg_start = NULL;
  6851.     vim_free(HL_TABLE()[idx].sg_stop);
  6852.     HL_TABLE()[idx].sg_stop = NULL;
  6853.     HL_TABLE()[idx].sg_term_attr = 0;
  6854.     HL_TABLE()[idx].sg_cterm = 0;
  6855.     HL_TABLE()[idx].sg_cterm_bold = FALSE;
  6856.     HL_TABLE()[idx].sg_cterm_fg = 0;
  6857.     HL_TABLE()[idx].sg_cterm_bg = 0;
  6858.     HL_TABLE()[idx].sg_cterm_attr = 0;
  6859. #ifdef FEAT_GUI        /* in non-GUI fonts are simply ignored */
  6860.     HL_TABLE()[idx].sg_gui = 0;
  6861.     HL_TABLE()[idx].sg_gui_fg = 0;
  6862.     vim_free(HL_TABLE()[idx].sg_gui_fg_name);
  6863.     HL_TABLE()[idx].sg_gui_fg_name = NULL;
  6864.     HL_TABLE()[idx].sg_gui_bg = 0;
  6865.     vim_free(HL_TABLE()[idx].sg_gui_bg_name);
  6866.     HL_TABLE()[idx].sg_gui_bg_name = NULL;
  6867.     gui_mch_free_font(HL_TABLE()[idx].sg_font);
  6868.     HL_TABLE()[idx].sg_font = NOFONT;
  6869. # ifdef FEAT_XFONTSET
  6870.     gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
  6871.     HL_TABLE()[idx].sg_fontset = NOFONTSET;
  6872. # endif
  6873.     vim_free(HL_TABLE()[idx].sg_font_name);
  6874.     HL_TABLE()[idx].sg_font_name = NULL;
  6875.     HL_TABLE()[idx].sg_gui_attr = 0;
  6876. #endif
  6877. }
  6878.  
  6879. #if defined(FEAT_GUI) || defined(PROTO)
  6880. /*
  6881.  * Set the normal foreground and background colors according to the "Normal"
  6882.  * highlighighting group.  For X11 also set "Menu", "Scrollbar", and
  6883.  * "Tooltip" colors.
  6884.  */
  6885.     void
  6886. set_normal_colors()
  6887. {
  6888.     if (set_group_colors((char_u *)"Normal",
  6889.                    &gui.norm_pixel, &gui.back_pixel, FALSE, TRUE,
  6890.                    FALSE))
  6891.     {
  6892.     gui_mch_new_colors();
  6893.     must_redraw = CLEAR;
  6894.     }
  6895. #ifdef FEAT_GUI_X11
  6896.     if (set_group_colors((char_u *)"Menu",
  6897.              &gui.menu_fg_pixel, &gui.menu_bg_pixel, TRUE, FALSE,
  6898.              FALSE))
  6899.     {
  6900. # ifdef FEAT_MENU
  6901.     gui_mch_new_menu_colors();
  6902. # endif
  6903.     must_redraw = CLEAR;
  6904.     }
  6905. # ifdef FEAT_BEVAL
  6906.     if (set_group_colors((char_u *)"Tooltip",
  6907.              &gui.tooltip_fg_pixel, &gui.tooltip_bg_pixel,
  6908.              FALSE, FALSE, TRUE))
  6909.     {
  6910. # ifdef FEAT_TOOLBAR
  6911.     gui_mch_new_tooltip_colors();
  6912. # endif
  6913.     must_redraw = CLEAR;
  6914.     }
  6915. #endif
  6916.     if (set_group_colors((char_u *)"Scrollbar",
  6917.             &gui.scroll_fg_pixel, &gui.scroll_bg_pixel, FALSE, FALSE,
  6918.             FALSE))
  6919.     {
  6920.     gui_new_scrollbar_colors();
  6921.     must_redraw = CLEAR;
  6922.     }
  6923. #endif
  6924. }
  6925.  
  6926. /*
  6927.  * Set the colors for "Normal", "Menu", "Tooltip" or "Scrollbar".
  6928.  */
  6929.     static int
  6930. set_group_colors(name, fgp, bgp, do_menu, use_norm, do_tooltip)
  6931.     char_u    *name;
  6932.     guicolor_T    *fgp;
  6933.     guicolor_T    *bgp;
  6934.     int        do_menu;
  6935.     int        use_norm;
  6936.     int        do_tooltip;
  6937. {
  6938.     int        idx;
  6939.  
  6940.     idx = syn_name2id(name) - 1;
  6941.     if (idx >= 0)
  6942.     {
  6943.     gui_do_one_color(idx, do_menu, do_tooltip);
  6944.  
  6945.     if (HL_TABLE()[idx].sg_gui_fg > 0)
  6946.         *fgp = HL_TABLE()[idx].sg_gui_fg - 1;
  6947.     else if (use_norm)
  6948.         *fgp = gui.def_norm_pixel;
  6949.     if (HL_TABLE()[idx].sg_gui_bg > 0)
  6950.         *bgp = HL_TABLE()[idx].sg_gui_bg - 1;
  6951.     else if (use_norm)
  6952.         *bgp = gui.def_back_pixel;
  6953.     return TRUE;
  6954.     }
  6955.     return FALSE;
  6956. }
  6957.  
  6958. /*
  6959.  * Get the font of the "Normal" group.
  6960.  * Returns "" when it's not found or not set.
  6961.  */
  6962.     char_u *
  6963. hl_get_font_name()
  6964. {
  6965.     int        id;
  6966.     char_u    *s;
  6967.  
  6968.     id = syn_name2id((char_u *)"Normal");
  6969.     if (id > 0)
  6970.     {
  6971.     s = HL_TABLE()[id - 1].sg_font_name;
  6972.     if (s != NULL)
  6973.         return s;
  6974.     }
  6975.     return (char_u *)"";
  6976. }
  6977.  
  6978. /*
  6979.  * Set font for "Normal" group.  Called by gui_mch_init_font() when a font has
  6980.  * actually chosen to be used.
  6981.  */
  6982.     void
  6983. hl_set_font_name(font_name)
  6984.     char_u    *font_name;
  6985. {
  6986.     int        id;
  6987.  
  6988.     id = syn_name2id((char_u *)"Normal");
  6989.     if (id > 0)
  6990.     {
  6991.     vim_free(HL_TABLE()[id - 1].sg_font_name);
  6992.     HL_TABLE()[id - 1].sg_font_name = vim_strsave(font_name);
  6993.     }
  6994. }
  6995.  
  6996. /*
  6997.  * Set background color for "Normal" group.  Called by gui_set_bg_color()
  6998.  * when the color is known.
  6999.  */
  7000.     void
  7001. hl_set_bg_color_name(name)
  7002.     char_u  *name;        /* must have been allocated */
  7003. {
  7004.     int        id;
  7005.  
  7006.     if (name != NULL)
  7007.     {
  7008.     id = syn_name2id((char_u *)"Normal");
  7009.     if (id > 0)
  7010.     {
  7011.         vim_free(HL_TABLE()[id - 1].sg_gui_bg_name);
  7012.         HL_TABLE()[id - 1].sg_gui_bg_name = name;
  7013.     }
  7014.     }
  7015. }
  7016.  
  7017. /*
  7018.  * Set foreground color for "Normal" group.  Called by gui_set_fg_color()
  7019.  * when the color is known.
  7020.  */
  7021.     void
  7022. hl_set_fg_color_name(name)
  7023.     char_u  *name;        /* must have been allocated */
  7024. {
  7025.     int        id;
  7026.  
  7027.     if (name != NULL)
  7028.     {
  7029.     id = syn_name2id((char_u *)"Normal");
  7030.     if (id > 0)
  7031.     {
  7032.         vim_free(HL_TABLE()[id - 1].sg_gui_fg_name);
  7033.         HL_TABLE()[id - 1].sg_gui_fg_name = name;
  7034.     }
  7035.     }
  7036. }
  7037.  
  7038. /*
  7039.  * Return the handle for a color name.
  7040.  * Returns -1 when failed.
  7041.  */
  7042.     static guicolor_T
  7043. color_name2handle(name)
  7044.     char_u  *name;
  7045. {
  7046.     if (STRCMP(name, "NONE") == 0)
  7047.     return (guicolor_T)-1;
  7048.  
  7049.     if (STRICMP(name, "fg") == 0 || STRICMP(name, "foreground") == 0)
  7050.     return gui.norm_pixel;
  7051.     if (STRICMP(name, "bg") == 0 || STRICMP(name, "background") == 0)
  7052.     return gui.back_pixel;
  7053.  
  7054.     return gui_mch_get_color(name);
  7055. }
  7056.  
  7057. /*
  7058.  * Return the handle for a font name.
  7059.  * Returns NOFONT when failed.
  7060.  */
  7061.     static GuiFont
  7062. font_name2handle(name)
  7063.     char_u  *name;
  7064. {
  7065.     if (STRCMP(name, "NONE") == 0)
  7066.     return NOFONT;
  7067.  
  7068.     return gui_mch_get_font(name, TRUE);
  7069. }
  7070.  
  7071. # ifdef FEAT_XFONTSET
  7072. /*
  7073.  * Return the handle for a fontset name.
  7074.  * Returns NOFONTSET when failed.
  7075.  */
  7076.     static GuiFontset
  7077. fontset_name2handle(name, fixed_width)
  7078.     char_u    *name;
  7079.     int        fixed_width;
  7080. {
  7081.     if (STRCMP(name, "NONE") == 0)
  7082.     return NOFONTSET;
  7083.  
  7084.     return gui_mch_get_fontset(name, TRUE, fixed_width);
  7085. }
  7086. # endif
  7087.  
  7088. /*
  7089.  * Get the font or fontset for one highlight group.
  7090.  */
  7091. /*ARGSUSED*/
  7092.     static void
  7093. hl_do_font(idx, arg, do_normal, do_menu, do_tooltip)
  7094.     int        idx;
  7095.     char_u    *arg;
  7096.     int        do_normal;    /* set normal font */
  7097.     int        do_menu;    /* set menu font */
  7098.     int        do_tooltip;    /* set tooltip font */
  7099. {
  7100. # ifdef FEAT_XFONTSET
  7101.     /* If 'guifontset' is not empty, first try using the name as a
  7102.      * fontset.  If that doesn't work, use it as a font name. */
  7103.     if (*p_guifontset != NUL
  7104. #  ifdef FONTSET_ALWAYS
  7105.     || do_menu
  7106. #  endif
  7107. #  ifdef FEAT_BEVAL
  7108.     /* In Athena & Motif, the Tooltip highlight group is always a fontset */
  7109.     || do_tooltip
  7110. #  endif
  7111.         )
  7112.     HL_TABLE()[idx].sg_fontset = fontset_name2handle(arg, 0
  7113. #  ifdef FONTSET_ALWAYS
  7114.         || do_menu
  7115. #  endif
  7116. #  ifdef FEAT_BEVAL
  7117.         || do_tooltip
  7118. #  endif
  7119.         );
  7120.     if (HL_TABLE()[idx].sg_fontset != NOFONTSET)
  7121.     {
  7122.     /* If it worked and it's the Normal group, use it as the
  7123.      * normal fontset.  Same for the Menu group. */
  7124.     if (do_normal)
  7125.         gui_init_font(arg, TRUE);
  7126. #   if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
  7127.     if (do_menu)
  7128.     {
  7129. #    ifdef FONTSET_ALWAYS
  7130.         gui.menu_fontset = HL_TABLE()[idx].sg_fontset;
  7131. #    else
  7132.         /* YIKES!  This is a bug waiting to crash the program */
  7133.         gui.menu_font = HL_TABLE()[idx].sg_fontset;
  7134. #    endif
  7135.         gui_mch_new_menu_font();
  7136.     }
  7137. #    ifdef FEAT_BEVAL
  7138.     if (do_tooltip)
  7139.     {
  7140.         /* The Athena widget set cannot currently handle switching between
  7141.          * displaying a single font and a fontset.
  7142.          * If the XtNinternational resource is set to True at widget
  7143.          * creation, then a fontset is always used, othwise an
  7144.          * XFontStruct is used.
  7145.          */
  7146.         gui.tooltip_fontset = (XFontSet)HL_TABLE()[idx].sg_fontset;
  7147.         gui_mch_new_tooltip_font();
  7148.     }
  7149. #    endif
  7150. #   endif
  7151.     }
  7152.     else
  7153. # endif
  7154.     {
  7155.     HL_TABLE()[idx].sg_font = font_name2handle(arg);
  7156.     /* If it worked and it's the Normal group, use it as the
  7157.      * normal font.  Same for the Menu group. */
  7158.     if (HL_TABLE()[idx].sg_font != NOFONT)
  7159.     {
  7160.         if (do_normal)
  7161.         gui_init_font(arg, FALSE);
  7162. #ifndef FONTSET_ALWAYS
  7163. # if (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_ATHENA)) && defined(FEAT_MENU)
  7164.         if (do_menu)
  7165.         {
  7166.         gui.menu_font = HL_TABLE()[idx].sg_font;
  7167.         gui_mch_new_menu_font();
  7168.         }
  7169. # endif
  7170. #endif
  7171.     }
  7172.     }
  7173. }
  7174.  
  7175. #endif /* FEAT_GUI */
  7176.  
  7177. /*
  7178.  * Table with the specifications for an attribute number.
  7179.  * Note that this table is used by ALL buffers.  This is required because the
  7180.  * GUI can redraw at any time for any buffer.
  7181.  */
  7182. garray_T    term_attr_table = {0, 0, 0, 0, NULL};
  7183.  
  7184. #define TERM_ATTR_ENTRY(idx) ((attrentry_T *)term_attr_table.ga_data)[idx]
  7185.  
  7186. garray_T    cterm_attr_table = {0, 0, 0, 0, NULL};
  7187.  
  7188. #define CTERM_ATTR_ENTRY(idx) ((attrentry_T *)cterm_attr_table.ga_data)[idx]
  7189.  
  7190. #ifdef FEAT_GUI
  7191. garray_T    gui_attr_table = {0, 0, 0, 0, NULL};
  7192.  
  7193. #define GUI_ATTR_ENTRY(idx) ((attrentry_T *)gui_attr_table.ga_data)[idx]
  7194. #endif
  7195.  
  7196. /*
  7197.  * Return the attr number for a set of colors and font.
  7198.  * Add a new entry to the term_attr_table, cterm_attr_table or gui_attr_table
  7199.  * if the combination is new.
  7200.  * Return 0 for error (no more room).
  7201.  */
  7202.     static int
  7203. get_attr_entry(table, aep)
  7204.     garray_T    *table;
  7205.     attrentry_T    *aep;
  7206. {
  7207.     int        i;
  7208.     attrentry_T    *gap;
  7209.     static int    recursive = FALSE;
  7210.  
  7211.     /*
  7212.      * Init the table, in case it wasn't done yet.
  7213.      */
  7214.     table->ga_itemsize = sizeof(attrentry_T);
  7215.     table->ga_growsize = 7;
  7216.  
  7217.     /*
  7218.      * Try to find an entry with the same specifications.
  7219.      */
  7220.     for (i = 0; i < table->ga_len; ++i)
  7221.     {
  7222.     gap = &(((attrentry_T *)table->ga_data)[i]);
  7223.     if (       aep->ae_attr == gap->ae_attr
  7224.         && (
  7225. #ifdef FEAT_GUI
  7226.                (table == &gui_attr_table
  7227.             && (aep->ae_u.gui.fg_color == gap->ae_u.gui.fg_color
  7228.                 && aep->ae_u.gui.bg_color == gap->ae_u.gui.bg_color
  7229.                 && aep->ae_u.gui.font == gap->ae_u.gui.font
  7230. #  ifdef FEAT_XFONTSET
  7231.                 && aep->ae_u.gui.fontset == gap->ae_u.gui.fontset
  7232. #  endif
  7233.                 ))
  7234.             ||
  7235. #endif
  7236.                (table == &term_attr_table
  7237.             && (aep->ae_u.term.start == NULL) ==
  7238.                         (gap->ae_u.term.start == NULL)
  7239.             && (aep->ae_u.term.start == NULL
  7240.                 || STRCMP(aep->ae_u.term.start,
  7241.                            gap->ae_u.term.start) == 0)
  7242.             && (aep->ae_u.term.stop == NULL) ==
  7243.                          (gap->ae_u.term.stop == NULL)
  7244.             && (aep->ae_u.term.stop == NULL
  7245.                 || STRCMP(aep->ae_u.term.stop,
  7246.                            gap->ae_u.term.stop) == 0))
  7247.             || (table == &cterm_attr_table
  7248.             && aep->ae_u.cterm.fg_color == gap->ae_u.cterm.fg_color
  7249.             && aep->ae_u.cterm.bg_color == gap->ae_u.cterm.bg_color)
  7250.              ))
  7251.  
  7252.     return i + ATTR_OFF;
  7253.     }
  7254.  
  7255.     if (table->ga_len + ATTR_OFF == 256)
  7256.     {
  7257.     /*
  7258.      * Running out of attribute entries!  remove all attributes, and
  7259.      * compute new ones for all groups.
  7260.      * When called recursively, we are really out of numbers.
  7261.      */
  7262.     if (recursive)
  7263.     {
  7264.         EMSG(_("E424: Too many different highlighting attributes in use"));
  7265.         return 0;
  7266.     }
  7267.     recursive = TRUE;
  7268.  
  7269. #ifdef FEAT_GUI
  7270.     ga_clear(&gui_attr_table);
  7271. #endif
  7272.     ga_clear(&term_attr_table);
  7273.     ga_clear(&cterm_attr_table);
  7274.     must_redraw = CLEAR;
  7275.  
  7276.     for (i = 0; i < highlight_ga.ga_len; ++i)
  7277.         set_hl_attr(i);
  7278.  
  7279.     recursive = FALSE;
  7280.     }
  7281.  
  7282.     /*
  7283.      * This is a new combination of colors and font, add an entry.
  7284.      */
  7285.     if (ga_grow(table, 1) == FAIL)
  7286.     return 0;
  7287.  
  7288.     gap = &(((attrentry_T *)table->ga_data)[table->ga_len]);
  7289.     vim_memset(gap, 0, sizeof(attrentry_T));
  7290.     gap->ae_attr = aep->ae_attr;
  7291. #ifdef FEAT_GUI
  7292.     if (table == &gui_attr_table)
  7293.     {
  7294.     gap->ae_u.gui.fg_color = aep->ae_u.gui.fg_color;
  7295.     gap->ae_u.gui.bg_color = aep->ae_u.gui.bg_color;
  7296.     gap->ae_u.gui.font = aep->ae_u.gui.font;
  7297. # ifdef FEAT_XFONTSET
  7298.     gap->ae_u.gui.fontset = aep->ae_u.gui.fontset;
  7299. # endif
  7300.     }
  7301. #endif
  7302.     if (table == &term_attr_table)
  7303.     {
  7304.     if (aep->ae_u.term.start == NULL)
  7305.         gap->ae_u.term.start = NULL;
  7306.     else
  7307.         gap->ae_u.term.start = vim_strsave(aep->ae_u.term.start);
  7308.     if (aep->ae_u.term.stop == NULL)
  7309.         gap->ae_u.term.stop = NULL;
  7310.     else
  7311.         gap->ae_u.term.stop = vim_strsave(aep->ae_u.term.stop);
  7312.     }
  7313.     else if (table == &cterm_attr_table)
  7314.     {
  7315.     gap->ae_u.cterm.fg_color = aep->ae_u.cterm.fg_color;
  7316.     gap->ae_u.cterm.bg_color = aep->ae_u.cterm.bg_color;
  7317.     }
  7318.     ++table->ga_len;
  7319.     --table->ga_room;
  7320.     return (table->ga_len - 1 + ATTR_OFF);
  7321. }
  7322.  
  7323. #ifdef FEAT_GUI
  7324.  
  7325.     attrentry_T *
  7326. syn_gui_attr2entry(attr)
  7327.     int            attr;
  7328. {
  7329.     attr -= ATTR_OFF;
  7330.     if (attr >= gui_attr_table.ga_len)        /* did ":syntax clear" */
  7331.     return NULL;
  7332.     return &(GUI_ATTR_ENTRY(attr));
  7333. }
  7334.  
  7335. #endif /* FEAT_GUI */
  7336.  
  7337.     attrentry_T *
  7338. syn_term_attr2entry(attr)
  7339.     int            attr;
  7340. {
  7341.     attr -= ATTR_OFF;
  7342.     if (attr >= term_attr_table.ga_len)        /* did ":syntax clear" */
  7343.     return NULL;
  7344.     return &(TERM_ATTR_ENTRY(attr));
  7345. }
  7346.  
  7347.     attrentry_T *
  7348. syn_cterm_attr2entry(attr)
  7349.     int            attr;
  7350. {
  7351.     attr -= ATTR_OFF;
  7352.     if (attr >= cterm_attr_table.ga_len)    /* did ":syntax clear" */
  7353.     return NULL;
  7354.     return &(CTERM_ATTR_ENTRY(attr));
  7355. }
  7356.  
  7357. #define LIST_ATTR   1
  7358. #define LIST_STRING 2
  7359. #define LIST_INT    3
  7360.  
  7361.     static void
  7362. highlight_list_one(id)
  7363.     int        id;
  7364. {
  7365.     struct hl_group    *sgp;
  7366.     int            didh = FALSE;
  7367.  
  7368.     sgp = &HL_TABLE()[id - 1];        /* index is ID minus one */
  7369.  
  7370.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7371.                     sgp->sg_term, NULL, "term");
  7372.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7373.                     0, sgp->sg_start, "start");
  7374.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7375.                     0, sgp->sg_stop, "stop");
  7376.  
  7377.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7378.                     sgp->sg_cterm, NULL, "cterm");
  7379.     didh = highlight_list_arg(id, didh, LIST_INT,
  7380.                     sgp->sg_cterm_fg, NULL, "ctermfg");
  7381.     didh = highlight_list_arg(id, didh, LIST_INT,
  7382.                     sgp->sg_cterm_bg, NULL, "ctermbg");
  7383.  
  7384. #ifdef FEAT_GUI
  7385.     didh = highlight_list_arg(id, didh, LIST_ATTR,
  7386.                     sgp->sg_gui, NULL, "gui");
  7387.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7388.                     0, sgp->sg_gui_fg_name, "guifg");
  7389.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7390.                     0, sgp->sg_gui_bg_name, "guibg");
  7391.     didh = highlight_list_arg(id, didh, LIST_STRING,
  7392.                     0, sgp->sg_font_name, "font");
  7393. #endif
  7394.  
  7395.     if (sgp->sg_link)
  7396.     {
  7397.     (void)syn_list_header(didh, 9999, id);
  7398.     msg_puts_attr((char_u *)"links to", hl_attr(HLF_D));
  7399.     msg_putchar(' ');
  7400.     msg_outtrans(HL_TABLE()[HL_TABLE()[id - 1].sg_link - 1].sg_name);
  7401.     }
  7402. }
  7403.  
  7404.     static int
  7405. highlight_list_arg(id, didh, type, iarg, sarg, name)
  7406.     int        id;
  7407.     int        didh;
  7408.     int        type;
  7409.     int        iarg;
  7410.     char_u    *sarg;
  7411.     char    *name;
  7412. {
  7413.     char_u    buf[100];
  7414.     char_u    *ts;
  7415.     int        i;
  7416.  
  7417.     if (type == LIST_STRING ? (sarg != NULL) : (iarg != 0))
  7418.     {
  7419.     ts = buf;
  7420.     if (type == LIST_INT)
  7421.         sprintf((char *)buf, "%d", iarg - 1);
  7422.     else if (type == LIST_STRING)
  7423.         ts = sarg;
  7424.     else /* type == LIST_ATTR */
  7425.     {
  7426.         buf[0] = NUL;
  7427.         for (i = 0; hl_attr_table[i] != 0; ++i)
  7428.         {
  7429.         if (iarg & hl_attr_table[i])
  7430.         {
  7431.             if (buf[0] != NUL)
  7432.             STRCAT(buf, ",");
  7433.             STRCAT(buf, hl_name_table[i]);
  7434.             iarg &= ~hl_attr_table[i];        /* don't want "inverse" */
  7435.         }
  7436.         }
  7437.     }
  7438.  
  7439.     (void)syn_list_header(didh,
  7440.                    (int)(vim_strsize(ts) + STRLEN(name) + 1), id);
  7441.     didh = TRUE;
  7442.  
  7443.     MSG_PUTS_ATTR(name, hl_attr(HLF_D));
  7444.     MSG_PUTS_ATTR("=", hl_attr(HLF_D));
  7445.     msg_outtrans(ts);
  7446.     }
  7447.     return didh;
  7448. }
  7449.  
  7450. #if (((defined(FEAT_EVAL) || defined(FEAT_PRINTER))) && defined(FEAT_SYN_HL)) || defined(PROTO)
  7451. /*
  7452.  * Return "1" if highlight group "id" has attribute "flag".
  7453.  * Return NULL otherwise.
  7454.  */
  7455.     char_u *
  7456. highlight_has_attr(id, flag, modec)
  7457.     int        id;
  7458.     int        flag;
  7459.     int        modec;    /* 'g' for GUI, 'c' for cterm, 't' for term */
  7460. {
  7461.     int        attr;
  7462.  
  7463.     if (id <= 0 || id > highlight_ga.ga_len)
  7464.     return NULL;
  7465.  
  7466. #ifdef FEAT_GUI
  7467.     if (modec == 'g')
  7468.     attr = HL_TABLE()[id - 1].sg_gui;
  7469.     else
  7470. #endif
  7471.      if (modec == 'c')
  7472.     attr = HL_TABLE()[id - 1].sg_cterm;
  7473.     else
  7474.     attr = HL_TABLE()[id - 1].sg_term;
  7475.  
  7476.     if (attr & flag)
  7477.     return (char_u *)"1";
  7478.     return NULL;
  7479. }
  7480. #endif
  7481.  
  7482. #if (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) || defined(PROTO)
  7483. /*
  7484.  * Return color name of highlight group "id".
  7485.  */
  7486.     char_u *
  7487. highlight_color(id, what, modec)
  7488.     int        id;
  7489.     char_u    *what;    /* "fg", "bg", "fg#" or "bg#" */
  7490.     int        modec;    /* 'g' for GUI, 'c' for cterm, 't' for term */
  7491. {
  7492.     static char_u    name[20];
  7493.     int            n;
  7494.     int            fg;
  7495.  
  7496.     if (id <= 0 || id > highlight_ga.ga_len)
  7497.     return NULL;
  7498.  
  7499.     if (TO_LOWER(what[0]) == 'f')
  7500.     fg = TRUE;
  7501.     else
  7502.     fg = FALSE;
  7503. #ifdef FEAT_GUI
  7504.     if (modec == 'g')
  7505.     {
  7506.     /* return #RRGGBB form (only possible when GUI is running) */
  7507.     if (gui.in_use && what[1] && what[2] == '#')
  7508.     {
  7509.         guicolor_T        color;
  7510.         long_u        rgb;
  7511.         static char_u    buf[10];
  7512.  
  7513.         if (fg)
  7514.         color = HL_TABLE()[id - 1].sg_gui_fg;
  7515.         else
  7516.         color = HL_TABLE()[id - 1].sg_gui_bg;
  7517.         if (color == 0)
  7518.         return NULL;
  7519.         rgb = gui_mch_get_rgb(color - 1);
  7520.         sprintf((char *)buf, "#%02x%02x%02x",
  7521.                       (unsigned)(rgb >> 16),
  7522.                       (unsigned)(rgb >> 8) & 255,
  7523.                       (unsigned)rgb & 255);
  7524.         return buf;
  7525.     }
  7526.     if (fg)
  7527.         return (HL_TABLE()[id - 1].sg_gui_fg_name);
  7528.     return (HL_TABLE()[id - 1].sg_gui_bg_name);
  7529.     }
  7530. #endif
  7531.     if (modec == 'c')
  7532.     {
  7533.     if (fg)
  7534.         n = HL_TABLE()[id - 1].sg_cterm_fg - 1;
  7535.     else
  7536.         n = HL_TABLE()[id - 1].sg_cterm_bg - 1;
  7537.     sprintf((char *)name, "%d", n);
  7538.     return name;
  7539.     }
  7540.     /* term doesn't have color */
  7541.     return NULL;
  7542. }
  7543. #endif
  7544.  
  7545. #if (defined(FEAT_SYN_HL) && defined(FEAT_GUI) && defined(FEAT_PRINTER)) \
  7546.     || defined(PROTO)
  7547. /*
  7548.  * Return color name of highlight group "id" as RGB value.
  7549.  */
  7550.     long_u
  7551. highlight_gui_color_rgb(id, fg)
  7552.     int        id;
  7553.     int        fg;    /* TRUE = fg, FALSE = bg */
  7554. {
  7555.     guicolor_T    color;
  7556.  
  7557.     if (id <= 0 || id > highlight_ga.ga_len)
  7558.     return 0L;
  7559.  
  7560.     if (fg)
  7561.     color = HL_TABLE()[id - 1].sg_gui_fg;
  7562.     else
  7563.     color = HL_TABLE()[id - 1].sg_gui_bg;
  7564.  
  7565.     if (color == 0)
  7566.     return 0L;
  7567.  
  7568.     return gui_mch_get_rgb(color - 1);
  7569. }
  7570. #endif
  7571.  
  7572. /*
  7573.  * Output the syntax list header.
  7574.  * Return TRUE when started a new line.
  7575.  */
  7576.     static int
  7577. syn_list_header(did_header, outlen, id)
  7578.     int        did_header;        /* did header already */
  7579.     int        outlen;        /* length of string that comes */
  7580.     int        id;            /* highlight group id */
  7581. {
  7582.     int        endcol = 19;
  7583.     int        newline = TRUE;
  7584.  
  7585.     if (!did_header)
  7586.     {
  7587.     msg_putchar('\n');
  7588.     msg_outtrans(HL_TABLE()[id - 1].sg_name);
  7589.     endcol = 15;
  7590.     }
  7591.     else if (msg_col + outlen + 1 >= Columns)
  7592.     msg_putchar('\n');
  7593.     else
  7594.     {
  7595.     if (msg_col >= endcol)    /* wrap around is like starting a new line */
  7596.         newline = FALSE;
  7597.     }
  7598.  
  7599.     if (msg_col >= endcol)    /* output at least one space */
  7600.     endcol = msg_col + 1;
  7601.     if (Columns <= endcol)    /* avoid hang for tiny window */
  7602.     endcol = Columns - 1;
  7603.  
  7604.     msg_advance(endcol);
  7605.  
  7606.     /* Show "xxx" with the attributes. */
  7607.     if (!did_header)
  7608.     {
  7609.     msg_puts_attr((char_u *)"xxx", syn_id2attr(id));
  7610.     msg_putchar(' ');
  7611.     }
  7612.  
  7613.     return newline;
  7614. }
  7615.  
  7616. /*
  7617.  * Set the attribute numbers for a highlight group.
  7618.  * Called after one of the attributes has changed.
  7619.  */
  7620.     static void
  7621. set_hl_attr(idx)
  7622.     int        idx;        /* index in array */
  7623. {
  7624.     attrentry_T    at_en;
  7625.  
  7626.     /* The "Normal" group doesn't need an attribute number */
  7627.     if (STRCMP(HL_TABLE()[idx].sg_name_u, "NORMAL") == 0)
  7628.     return;
  7629.  
  7630. #ifdef FEAT_GUI
  7631.     /*
  7632.      * For the GUI mode: If there are other than "normal" highlighting
  7633.      * attributes, need to allocate an attr number.
  7634.      */
  7635.     if (HL_TABLE()[idx].sg_gui_fg == 0
  7636.         && HL_TABLE()[idx].sg_gui_bg == 0
  7637.         && HL_TABLE()[idx].sg_font == NOFONT
  7638. # ifdef FEAT_XFONTSET
  7639.         && HL_TABLE()[idx].sg_fontset == NOFONTSET
  7640. # endif
  7641.         )
  7642.     {
  7643.     HL_TABLE()[idx].sg_gui_attr = HL_TABLE()[idx].sg_gui;
  7644.     }
  7645.     else
  7646.     {
  7647.     at_en.ae_attr = HL_TABLE()[idx].sg_gui;
  7648.     at_en.ae_u.gui.fg_color = HL_TABLE()[idx].sg_gui_fg;
  7649.     at_en.ae_u.gui.bg_color = HL_TABLE()[idx].sg_gui_bg;
  7650.     at_en.ae_u.gui.font = HL_TABLE()[idx].sg_font;
  7651. # ifdef FEAT_XFONTSET
  7652.     at_en.ae_u.gui.fontset = HL_TABLE()[idx].sg_fontset;
  7653. # endif
  7654.     HL_TABLE()[idx].sg_gui_attr = get_attr_entry(&gui_attr_table, &at_en);
  7655.     }
  7656. #endif
  7657.     /*
  7658.      * For the term mode: If there are other than "normal" highlighting
  7659.      * attributes, need to allocate an attr number.
  7660.      */
  7661.     if (HL_TABLE()[idx].sg_start == NULL && HL_TABLE()[idx].sg_stop == NULL)
  7662.     HL_TABLE()[idx].sg_term_attr = HL_TABLE()[idx].sg_term;
  7663.     else
  7664.     {
  7665.     at_en.ae_attr = HL_TABLE()[idx].sg_term;
  7666.     at_en.ae_u.term.start = HL_TABLE()[idx].sg_start;
  7667.     at_en.ae_u.term.stop = HL_TABLE()[idx].sg_stop;
  7668.     HL_TABLE()[idx].sg_term_attr = get_attr_entry(&term_attr_table, &at_en);
  7669.     }
  7670.  
  7671.     /*
  7672.      * For the color term mode: If there are other than "normal"
  7673.      * highlighting attributes, need to allocate an attr number.
  7674.      */
  7675.     if (HL_TABLE()[idx].sg_cterm_fg == 0 && HL_TABLE()[idx].sg_cterm_bg == 0)
  7676.     HL_TABLE()[idx].sg_cterm_attr = HL_TABLE()[idx].sg_cterm;
  7677.     else
  7678.     {
  7679.     at_en.ae_attr = HL_TABLE()[idx].sg_cterm;
  7680.     at_en.ae_u.cterm.fg_color = HL_TABLE()[idx].sg_cterm_fg;
  7681.     at_en.ae_u.cterm.bg_color = HL_TABLE()[idx].sg_cterm_bg;
  7682.     HL_TABLE()[idx].sg_cterm_attr =
  7683.                     get_attr_entry(&cterm_attr_table, &at_en);
  7684.     }
  7685. }
  7686.  
  7687. /*
  7688.  * Lookup a highlight group name and return it's ID.
  7689.  * If it is not found, 0 is returned.
  7690.  */
  7691.     int
  7692. syn_name2id(name)
  7693.     char_u    *name;
  7694. {
  7695.     int        i;
  7696.     char_u    name_u[200];
  7697.  
  7698.     /* Avoid using stricmp() too much, it's slow on some systems */
  7699.     /* Avoid alloc()/free(), these are slow too.  ID names over 200 chars
  7700.      * don't deserve to be found! */
  7701.     STRNCPY(name_u, name, 199);
  7702.     name_u[199] = NUL;
  7703.     vim_strup(name_u);
  7704.     for (i = highlight_ga.ga_len; --i >= 0; )
  7705.     if (HL_TABLE()[i].sg_name_u != NULL
  7706.         && STRCMP(name_u, HL_TABLE()[i].sg_name_u) == 0)
  7707.         break;
  7708.     return i + 1;
  7709. }
  7710.  
  7711. #if defined(FEAT_EVAL) || defined(PROTO)
  7712. /*
  7713.  * Return TRUE if highlight group "name" exists.
  7714.  */
  7715.     int
  7716. highlight_exists(name)
  7717.     char_u    *name;
  7718. {
  7719.     return (syn_name2id(name) > 0);
  7720. }
  7721. #endif
  7722.  
  7723. /*
  7724.  * Like syn_name2id(), but take a pointer + length argument.
  7725.  */
  7726.     int
  7727. syn_namen2id(linep, len)
  7728.     char_u  *linep;
  7729.     int        len;
  7730. {
  7731.     char_u  *name;
  7732.     int        id = 0;
  7733.  
  7734.     name = vim_strnsave(linep, len);
  7735.     if (name != NULL)
  7736.     {
  7737.     id = syn_name2id(name);
  7738.     vim_free(name);
  7739.     }
  7740.     return id;
  7741. }
  7742.  
  7743. /*
  7744.  * Find highlight group name in the table and return it's ID.
  7745.  * The argument is a pointer to the name and the length of the name.
  7746.  * If it doesn't exist yet, a new entry is created.
  7747.  * Return 0 for failure.
  7748.  */
  7749.     int
  7750. syn_check_group(pp, len)
  7751.     char_u        *pp;
  7752.     int            len;
  7753. {
  7754.     int        id;
  7755.     char_u  *name;
  7756.  
  7757.     name = vim_strnsave(pp, len);
  7758.     if (name == NULL)
  7759.     return 0;
  7760.  
  7761.     id = syn_name2id(name);
  7762.     if (id == 0)            /* doesn't exist yet */
  7763.     id = syn_add_group(name);
  7764.     else
  7765.     vim_free(name);
  7766.     return id;
  7767. }
  7768.  
  7769. /*
  7770.  * Add new highlight group and return it's ID.
  7771.  * "name" must be an allocated string, it will be consumed.
  7772.  * Return 0 for failure.
  7773.  */
  7774.     static int
  7775. syn_add_group(name)
  7776.     char_u        *name;
  7777. {
  7778.     /*
  7779.      * First call for this growarray: init growing array.
  7780.      */
  7781.     if (highlight_ga.ga_data == NULL)
  7782.     {
  7783.     highlight_ga.ga_itemsize = sizeof(struct hl_group);
  7784.     highlight_ga.ga_growsize = 10;
  7785.     }
  7786.  
  7787.     /*
  7788.      * Make room for at least one other syntax_highlight entry.
  7789.      */
  7790.     if (ga_grow(&highlight_ga, 1) == FAIL)
  7791.     {
  7792.     vim_free(name);
  7793.     return 0;
  7794.     }
  7795.  
  7796.     vim_memset(&(HL_TABLE()[highlight_ga.ga_len]), 0, sizeof(struct hl_group));
  7797.     HL_TABLE()[highlight_ga.ga_len].sg_name = name;
  7798.     HL_TABLE()[highlight_ga.ga_len].sg_name_u = vim_strsave_up(name);
  7799.     ++highlight_ga.ga_len;
  7800.     --highlight_ga.ga_room;
  7801.  
  7802.     return highlight_ga.ga_len;            /* ID is index plus one */
  7803. }
  7804.  
  7805. /*
  7806.  * When, just after calling syn_add_group(), an error is discovered, this
  7807.  * function deletes the new name.
  7808.  */
  7809.     static void
  7810. syn_unadd_group()
  7811. {
  7812.     --highlight_ga.ga_len;
  7813.     ++highlight_ga.ga_room;
  7814.     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name);
  7815.     vim_free(HL_TABLE()[highlight_ga.ga_len].sg_name_u);
  7816. }
  7817.  
  7818. /*
  7819.  * Translate a group ID to highlight attributes.
  7820.  */
  7821.     int
  7822. syn_id2attr(hl_id)
  7823.     int            hl_id;
  7824. {
  7825.     int            attr;
  7826.     struct hl_group    *sgp;
  7827.  
  7828.     hl_id = syn_get_final_id(hl_id);
  7829.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7830.  
  7831. #ifdef FEAT_GUI
  7832.     /*
  7833.      * Only use GUI attr when the GUI is being used.
  7834.      */
  7835.     if (gui.in_use)
  7836.     attr = sgp->sg_gui_attr;
  7837.     else
  7838. #endif
  7839.     if (t_colors > 1)
  7840.         attr = sgp->sg_cterm_attr;
  7841.     else
  7842.         attr = sgp->sg_term_attr;
  7843.  
  7844.     return attr;
  7845. }
  7846.  
  7847. #ifdef FEAT_GUI
  7848. /*
  7849.  * Get the GUI colors and attributes for a group ID.
  7850.  * NOTE: the colors will be 0 when not set, the color plus one otherwise.
  7851.  */
  7852.     int
  7853. syn_id2colors(hl_id, fgp, bgp)
  7854.     int        hl_id;
  7855.     guicolor_T    *fgp;
  7856.     guicolor_T    *bgp;
  7857. {
  7858.     struct hl_group    *sgp;
  7859.  
  7860.     hl_id = syn_get_final_id(hl_id);
  7861.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7862.  
  7863.     *fgp = sgp->sg_gui_fg;
  7864.     *bgp = sgp->sg_gui_bg;
  7865.     return sgp->sg_gui;
  7866. }
  7867. #endif
  7868.  
  7869. /*
  7870.  * Translate a group ID to the final group ID (following links).
  7871.  */
  7872.     int
  7873. syn_get_final_id(hl_id)
  7874.     int            hl_id;
  7875. {
  7876.     int            count;
  7877.     struct hl_group    *sgp;
  7878.  
  7879.     if (hl_id > highlight_ga.ga_len || hl_id < 1)
  7880.     return 0;            /* Can be called from eval!! */
  7881.  
  7882.     /*
  7883.      * Follow links until there is no more.
  7884.      * Look out for loops!  Break after 100 links.
  7885.      */
  7886.     for (count = 100; --count >= 0; )
  7887.     {
  7888.     sgp = &HL_TABLE()[hl_id - 1];        /* index is ID minus one */
  7889.     if (sgp->sg_link == 0 || sgp->sg_link > highlight_ga.ga_len)
  7890.         break;
  7891.     hl_id = sgp->sg_link;
  7892.     }
  7893.  
  7894.     return hl_id;
  7895. }
  7896.  
  7897. #ifdef FEAT_GUI
  7898. /*
  7899.  * Call this function just after the GUI has started.
  7900.  * It finds the font and color handles for the highlighting groups.
  7901.  */
  7902.     void
  7903. highlight_gui_started()
  7904. {
  7905.     int        idx;
  7906.  
  7907.     /* First get the colors from the "Normal" and "Menu" group, if set */
  7908.     set_normal_colors();
  7909.  
  7910.     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  7911.     gui_do_one_color(idx, FALSE, FALSE);
  7912.  
  7913.     highlight_changed();
  7914. }
  7915.  
  7916.     static void
  7917. gui_do_one_color(idx, do_menu, do_tooltip)
  7918.     int        idx;
  7919.     int        do_menu;    /* TRUE: might set the menu font */
  7920.     int        do_tooltip;    /* TRUE: might set the tooltip font */
  7921. {
  7922.     int        didit = FALSE;
  7923.  
  7924.     if (HL_TABLE()[idx].sg_font_name != NULL)
  7925.     {
  7926.     hl_do_font(idx, HL_TABLE()[idx].sg_font_name, FALSE, do_menu,
  7927.            do_tooltip);
  7928.     didit = TRUE;
  7929.     }
  7930.     if (HL_TABLE()[idx].sg_gui_fg_name != NULL)
  7931.     {
  7932.     HL_TABLE()[idx].sg_gui_fg =
  7933.         color_name2handle(HL_TABLE()[idx].sg_gui_fg_name) + 1;
  7934.     didit = TRUE;
  7935.     }
  7936.     if (HL_TABLE()[idx].sg_gui_bg_name != NULL)
  7937.     {
  7938.     HL_TABLE()[idx].sg_gui_bg =
  7939.         color_name2handle(HL_TABLE()[idx].sg_gui_bg_name) + 1;
  7940.     didit = TRUE;
  7941.     }
  7942.     if (didit)    /* need to get a new attr number */
  7943.     set_hl_attr(idx);
  7944. }
  7945.  
  7946. #endif
  7947.  
  7948. /*
  7949.  * Translate the 'highlight' option into attributes in highlight_attr[] and
  7950.  * set up the user highlights User1..9.  If FEAT_STL_OPT is in use, a set of
  7951.  * corresponding highlights to use on top of HLF_SNC is computed.
  7952.  * Called only when the 'highlight' option has been changed and upon first
  7953.  * screen redraw after any :highlight command.
  7954.  * Return FAIL when an invalid flag is found in 'highlight'.  OK otherwise.
  7955.  */
  7956.     int
  7957. highlight_changed()
  7958. {
  7959.     int        hlf;
  7960.     int        i;
  7961.     char_u    *p;
  7962.     int        attr;
  7963.     char_u    *end;
  7964.     int        id;
  7965. #ifdef USER_HIGHLIGHT
  7966.     char_u      userhl[10];
  7967. # ifdef FEAT_STL_OPT
  7968.     int        id_SNC = -1;
  7969.     int        id_S = -1;
  7970.     int        hlcnt;
  7971. # endif
  7972. #endif
  7973.     static int    hl_flags[HLF_COUNT] = HL_FLAGS;
  7974.  
  7975.     need_highlight_changed = FALSE;
  7976.  
  7977.     /*
  7978.      * Clear all attributes.
  7979.      */
  7980.     for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
  7981.     highlight_attr[hlf] = 0;
  7982.  
  7983.     /*
  7984.      * First set all attributes to their default value.
  7985.      * Then use the attributes from the 'highlight' option.
  7986.      */
  7987.     for (i = 0; i < 2; ++i)
  7988.     {
  7989.     if (i)
  7990.         p = p_hl;
  7991.     else
  7992.         p = get_highlight_default();
  7993.     if (p == NULL)        /* just in case */
  7994.         continue;
  7995.  
  7996.     while (*p)
  7997.     {
  7998.         for (hlf = 0; hlf < (int)HLF_COUNT; ++hlf)
  7999.         if (hl_flags[hlf] == *p)
  8000.             break;
  8001.         ++p;
  8002.         if (hlf == (int)HLF_COUNT || *p == NUL)
  8003.         return FAIL;
  8004.  
  8005.         /*
  8006.          * Allow several hl_flags to be combined, like "bu" for
  8007.          * bold-underlined.
  8008.          */
  8009.         attr = 0;
  8010.         for ( ; *p && *p != ','; ++p)        /* parse upto comma */
  8011.         {
  8012.         if (vim_iswhite(*p))            /* ignore white space */
  8013.             continue;
  8014.  
  8015.         if (attr > HL_ALL)  /* Combination with ':' is not allowed. */
  8016.             return FAIL;
  8017.  
  8018.         switch (*p)
  8019.         {
  8020.             case 'b':    attr |= HL_BOLD;
  8021.                 break;
  8022.             case 'i':    attr |= HL_ITALIC;
  8023.                 break;
  8024.             case '-':
  8025.             case 'n':                /* no highlighting */
  8026.                 break;
  8027.             case 'r':    attr |= HL_INVERSE;
  8028.                 break;
  8029.             case 's':    attr |= HL_STANDOUT;
  8030.                 break;
  8031.             case 'u':    attr |= HL_UNDERLINE;
  8032.                 break;
  8033.             case ':':    ++p;            /* highlight group name */
  8034.                 if (attr || *p == NUL)     /* no combinations */
  8035.                     return FAIL;
  8036.                 end = vim_strchr(p, ',');
  8037.                 if (end == NULL)
  8038.                     end = p + STRLEN(p);
  8039.                 id = syn_check_group(p, (int)(end - p));
  8040.                 if (id == 0)
  8041.                     return FAIL;
  8042.                 attr = syn_id2attr(id);
  8043.                 p = end - 1;
  8044. #if defined(FEAT_STL_OPT) && defined(USER_HIGHLIGHT)
  8045.                 if (hlf == (int)HLF_SNC)
  8046.                     id_SNC = syn_get_final_id(id);
  8047.                 else if (hlf == (int)HLF_S)
  8048.                     id_S = syn_get_final_id(id);
  8049. #endif
  8050.                 break;
  8051.             default:    return FAIL;
  8052.         }
  8053.         }
  8054.         highlight_attr[hlf] = attr;
  8055.  
  8056.         p = skip_to_option_part(p);        /* skip comma and spaces */
  8057.     }
  8058.     }
  8059.  
  8060. #ifdef USER_HIGHLIGHT
  8061.     /* Setup the user highlights
  8062.      *
  8063.      * Temporarily  utilize 10 more hl entries.  Have to be in there
  8064.      * simultaneously in case of table overflows in get_attr_entry()
  8065.      */
  8066. # ifdef FEAT_STL_OPT
  8067.     if (ga_grow(&highlight_ga, 10) == FAIL)
  8068.     return FAIL;
  8069.     hlcnt = highlight_ga.ga_len;
  8070.     if (id_S == 0)
  8071.     {            /* Make sure id_S is always valid to simplify code below */
  8072.     memset(&HL_TABLE()[hlcnt + 9], 0, sizeof(struct hl_group));
  8073.     HL_TABLE()[hlcnt + 9].sg_term = highlight_attr[HLF_S];
  8074.     id_S = hlcnt + 10;
  8075.     }
  8076. # endif
  8077.     for (i = 0; i < 9; i++)
  8078.     {
  8079.     sprintf((char *)userhl, "User%d", i + 1);
  8080.     id = syn_name2id(userhl);
  8081.     if (id == 0)
  8082.     {
  8083.         highlight_user[i] = 0;
  8084. # ifdef FEAT_STL_OPT
  8085.         highlight_stlnc[i] = 0;
  8086. # endif
  8087.     }
  8088.     else
  8089.     {
  8090. # ifdef FEAT_STL_OPT
  8091.         struct hl_group *hlt = HL_TABLE();
  8092. # endif
  8093.  
  8094.         highlight_user[i] = syn_id2attr(id);
  8095. # ifdef FEAT_STL_OPT
  8096.         if (id_SNC == 0)
  8097.         {
  8098.         memset(&hlt[hlcnt + i], 0, sizeof(struct hl_group));
  8099.         hlt[hlcnt + i].sg_term = highlight_attr[HLF_SNC];
  8100.         hlt[hlcnt + i].sg_cterm = highlight_attr[HLF_SNC];
  8101. #  ifdef FEAT_GUI
  8102.         hlt[hlcnt + i].sg_gui = highlight_attr[HLF_SNC];
  8103. #  endif
  8104.         }
  8105.         else
  8106.         mch_memmove(&hlt[hlcnt + i],
  8107.                 &hlt[id_SNC - 1],
  8108.                 sizeof(struct hl_group));
  8109.         hlt[hlcnt + i].sg_link = 0;
  8110.  
  8111.         /* Apply difference between UserX and HLF_S to HLF_SNC */
  8112.         hlt[hlcnt + i].sg_term ^=
  8113.         hlt[id - 1].sg_term ^ hlt[id_S - 1].sg_term;
  8114.         if (hlt[id - 1].sg_start != hlt[id_S - 1].sg_start)
  8115.         hlt[hlcnt + i].sg_start = hlt[id - 1].sg_start;
  8116.         if (hlt[id - 1].sg_stop != hlt[id_S - 1].sg_stop)
  8117.         hlt[hlcnt + i].sg_stop = hlt[id - 1].sg_stop;
  8118.         hlt[hlcnt + i].sg_cterm ^=
  8119.         hlt[id - 1].sg_cterm ^ hlt[id_S - 1].sg_cterm;
  8120.         if (hlt[id - 1].sg_cterm_fg != hlt[id_S - 1].sg_cterm_fg)
  8121.         hlt[hlcnt + i].sg_cterm_fg = hlt[id - 1].sg_cterm_fg;
  8122.         if (hlt[id - 1].sg_cterm_bg != hlt[id_S - 1].sg_cterm_bg)
  8123.         hlt[hlcnt + i].sg_cterm_bg = hlt[id - 1].sg_cterm_bg;
  8124. #  ifdef FEAT_GUI
  8125.         hlt[hlcnt + i].sg_gui ^=
  8126.         hlt[id - 1].sg_gui ^ hlt[id_S - 1].sg_gui;
  8127.         if (hlt[id - 1].sg_gui_fg != hlt[id_S - 1].sg_gui_fg)
  8128.         hlt[hlcnt + i].sg_gui_fg = hlt[id - 1].sg_gui_fg;
  8129.         if (hlt[id - 1].sg_gui_bg != hlt[id_S - 1].sg_gui_bg)
  8130.         hlt[hlcnt + i].sg_gui_bg = hlt[id - 1].sg_gui_bg;
  8131.         if (hlt[id - 1].sg_font != hlt[id_S - 1].sg_font)
  8132.         hlt[hlcnt + i].sg_font = hlt[id - 1].sg_font;
  8133. #   ifdef FEAT_XFONTSET
  8134.         if (hlt[id - 1].sg_fontset != hlt[id_S - 1].sg_fontset)
  8135.         hlt[hlcnt + i].sg_fontset = hlt[id - 1].sg_fontset;
  8136. #   endif
  8137. #  endif
  8138.         highlight_ga.ga_len = hlcnt + i + 1;
  8139.         set_hl_attr(hlcnt + i);    /* At long last we can apply */
  8140.         highlight_stlnc[i] = syn_id2attr(hlcnt + i + 1);
  8141. # endif
  8142.     }
  8143.     }
  8144. # ifdef FEAT_STL_OPT
  8145.     highlight_ga.ga_len = hlcnt;
  8146. # endif
  8147.  
  8148. #endif /* USER_HIGHLIGHT */
  8149.  
  8150.     return OK;
  8151. }
  8152.  
  8153. #ifdef FEAT_CMDL_COMPL
  8154.  
  8155. static void highlight_list __ARGS((void));
  8156. static void highlight_list_two __ARGS((int cnt, int attr));
  8157.  
  8158. /*
  8159.  * Handle command line completion for :highlight command.
  8160.  */
  8161.     void
  8162. set_context_in_highlight_cmd(xp, arg)
  8163.     expand_T    *xp;
  8164.     char_u    *arg;
  8165. {
  8166.     char_u    *p;
  8167.  
  8168.     /* Default: expand group names */
  8169.     xp->xp_context = EXPAND_HIGHLIGHT;
  8170.     xp->xp_pattern = arg;
  8171.     include_link = TRUE;
  8172.     include_default = TRUE;
  8173.  
  8174.     /* (part of) subcommand already typed */
  8175.     if (*arg != NUL)
  8176.     {
  8177.     p = skiptowhite(arg);
  8178.     if (*p != NUL)            /* past "default" or group name */
  8179.     {
  8180.         include_default = FALSE;
  8181.         if (STRNCMP("default", arg, p - arg) == 0)
  8182.         {
  8183.         arg = skipwhite(p);
  8184.         xp->xp_pattern = arg;
  8185.         p = skiptowhite(arg);
  8186.         }
  8187.         if (*p != NUL)            /* past group name */
  8188.         {
  8189.         include_link = FALSE;
  8190.         if (arg[1] == 'i' && arg[0] == 'N')
  8191.             highlight_list();
  8192.         if (STRNCMP("link", arg, p - arg) == 0
  8193.             || STRNCMP("clear", arg, p - arg) == 0)
  8194.         {
  8195.             xp->xp_pattern = skipwhite(p);
  8196.             p = skiptowhite(xp->xp_pattern);
  8197.             if (*p != NUL)        /* past first group name */
  8198.             {
  8199.             xp->xp_pattern = skipwhite(p);
  8200.             p = skiptowhite(xp->xp_pattern);
  8201.             }
  8202.         }
  8203.         if (*p != NUL)            /* past group name(s) */
  8204.             xp->xp_context = EXPAND_NOTHING;
  8205.         }
  8206.     }
  8207.     }
  8208. }
  8209.  
  8210. /*
  8211.  * List highlighting matches in a nice way.
  8212.  */
  8213.     static void
  8214. highlight_list()
  8215. {
  8216.     int        i;
  8217.  
  8218.     for (i = 10; --i >= 0; )
  8219.     highlight_list_two(i, hl_attr(HLF_D));
  8220.     for (i = 40; --i >= 0; )
  8221.     highlight_list_two(99, 0);
  8222. }
  8223.  
  8224.     static void
  8225. highlight_list_two(cnt, attr)
  8226.     int        cnt;
  8227.     int        attr;
  8228. {
  8229.     msg_puts_attr((char_u *)("N \bI \b!  \b" + cnt / 11), attr);
  8230.     msg_clr_eos();
  8231.     out_flush();
  8232.     ui_delay(cnt == 99 ? 40L : (long)cnt * 50L, FALSE);
  8233. }
  8234.  
  8235. #endif /* FEAT_CMDL_COMPL */
  8236.  
  8237. #if defined(FEAT_CMDL_COMPL) || (defined(FEAT_SYN_HL) && defined(FEAT_EVAL)) \
  8238.     || defined(FEAT_SIGNS) || defined(PROTO)
  8239. /*
  8240.  * Function given to ExpandGeneric() to obtain the list of group names.
  8241.  * Also used for synIDattr() function.
  8242.  */
  8243. /*ARGSUSED*/
  8244.     char_u *
  8245. get_highlight_name(xp, idx)
  8246.     expand_T    *xp;
  8247.     int        idx;
  8248. {
  8249.     if (idx == highlight_ga.ga_len
  8250. #ifdef FEAT_CMDL_COMPL
  8251.         && include_link
  8252. #endif
  8253.         )
  8254.     return (char_u *)"link";
  8255.     if (idx == highlight_ga.ga_len + 1
  8256. #ifdef FEAT_CMDL_COMPL
  8257.         && include_link
  8258. #endif
  8259.         )
  8260.     return (char_u *)"clear";
  8261.     if (idx == highlight_ga.ga_len + 2
  8262. #ifdef FEAT_CMDL_COMPL
  8263.         && include_default
  8264. #endif
  8265.         )
  8266.     return (char_u *)"default";
  8267.     if (idx < 0 || idx >= highlight_ga.ga_len)
  8268.     return NULL;
  8269.     return HL_TABLE()[idx].sg_name;
  8270. }
  8271. #endif
  8272.  
  8273. #ifdef FEAT_GUI
  8274. /*
  8275.  * Free all the highlight group fonts.
  8276.  * Used when quitting for systems which need it.
  8277.  */
  8278.     void
  8279. free_highlight_fonts()
  8280. {
  8281.     int        idx;
  8282.  
  8283.     for (idx = 0; idx < highlight_ga.ga_len; ++idx)
  8284.     {
  8285.     gui_mch_free_font(HL_TABLE()[idx].sg_font);
  8286.     HL_TABLE()[idx].sg_font = NOFONT;
  8287. # ifdef FEAT_XFONTSET
  8288.     gui_mch_free_fontset(HL_TABLE()[idx].sg_fontset);
  8289.     HL_TABLE()[idx].sg_fontset = NOFONTSET;
  8290. # endif
  8291.     }
  8292.  
  8293.     gui_mch_free_font(gui.norm_font);
  8294. # ifdef FEAT_XFONTSET
  8295.     gui_mch_free_fontset(gui.fontset);
  8296. # endif
  8297.     gui_mch_free_font(gui.bold_font);
  8298.     gui_mch_free_font(gui.ital_font);
  8299.     gui_mch_free_font(gui.boldital_font);
  8300. }
  8301. #endif
  8302.  
  8303. /**************************************
  8304.  *  End of Highlighting stuff          *
  8305.  **************************************/
  8306.